How can I do an atomic write/append in C#, or how do I get files opened with the FILE_APPEND_DATA flag?

后端 未结 3 1811
鱼传尺愫
鱼传尺愫 2020-12-28 17:13

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

相关标签:
3条回答
  • 2020-12-28 17:24

    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);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-28 17:26

    Why can't you use

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

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

    0 讨论(0)
  • 2020-12-28 17:37

    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.

    0 讨论(0)
提交回复
热议问题