22

Under most Unixes and Posix conforming operating systems performing an open() operating system call with the O_APPEND indicates to the OS that writes are to be atomic append and write operations. With this behavior,for local filesystems when you do a write, you know it get appended to the end of the file.

The Windows operating systems support the same functionality by passing FILE_APPEND_DATA in the appropriate parameter to the Win32 CreateFile() system call.

references:

http://www.google.com/search?q=msdn+createfile
or: http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

http://www.google.com/search?q=msdn+IoCreateFileSpecifyDeviceObjectHint
or: http://www.google.com/search?q=msdn+IoCreateFileSpecifyDeviceObjectHint

My problem is this, I cannot determine how to get this behavior under C# using the Net Framework Libraries, is there a way to get such behavior using the Net Framework? I do not believe using FileMode.Append gives such behavior, by the way.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Cameron
  • 2,903
  • 1
  • 30
  • 31

3 Answers3

40

Use one of the overloads of the FileStream constructor:

new FileStream(FileName, FileMode.Open, FileSystemRights.AppendData,
            FileShare.Write, 4096, FileOptions.None)

FileSystemRights.AppendData corresponds with FILE_APPEND_DATA

FileStream seems to insist on buffering, so make sure the buffer is large enough for each write and call Flush() after each write.

Tiny example:

    private void button1_Click(object sender, EventArgs e) {
        Thread t1 = new Thread(DoIt);
        Thread t2 = new Thread(DoIt);
        t1.Start("a");
        t2.Start("b");
        Thread.Sleep(2000);
        Environment.Exit(0);
    }

    private void DoIt(object p) {
        using (FileStream fs = new FileStream(FileName, FileMode.Open, FileSystemRights.AppendData,
            FileShare.Write, 4096, FileOptions.None)) {
            using (StreamWriter writer = new StreamWriter(fs)) {
                writer.AutoFlush = true;
                for (int i = 0; i < 20; ++i)
                    writer.WriteLine("{0}: {1:D3} {2:o} hello", p, i, DateTime.Now);
            }
        }
    }
Fred Mol
  • 631
  • 7
  • 4
  • 1
    Is the same as using the `File.AppendText` API? – Ben Jun 18 '13 at 13:04
  • The last parameter of the FileStream constructor, 'bufferSize', or 4096 in this example will be an upper bound on the length of an atomic write. – Cameron Mar 20 '15 at 21:18
  • Apparently FileSystemRights is not in dotnetstandard 2.0. – silvalli Mar 25 '19 at 07:17
  • Does it matter if you use FileMode.Append or FileMode.Open? Open will not create the file if it does not exist – Hoppe Feb 09 '21 at 22:00
  • 1
    @silvalli You can use [FileSystemAclExtensions.Create()](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemaclextensions.create?view=net-6.0#system-io-filesystemaclextensions-create(system-io-fileinfo-system-io-filemode-system-security-accesscontrol-filesystemrights-system-io-fileshare-system-int32-system-io-fileoptions-system-security-accesscontrol-filesecurity)) in .NET Core 3+ – Gabriel Luci Sep 07 '22 at 02:24
1

You can call CreateFile using PInvoke with the required parameters and pass the resulting handle to one of the FileStream Constructors which takes SafeFileHandle as a parameter.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
-3

Why can't you use

System.IO.File.AppendAllText("C:\\somefile.txt", "some content");

? That's a thread-safe/"atomic" call as well.

enderminh
  • 166
  • 1
  • 6
  • 5
    This unfortunatly doesn't give the process-wise atomic operation i'm looking for. – Cameron Dec 14 '09 at 22:52
  • 1
    Throws an IOException with File is being used by another process when multiple threads access the file. – IvanH Apr 16 '18 at 07:19