如何在C#中进行原子写入/追加,或者如何使用FILE_APPEND_DATA标志打开文件?

时间:2022-05-12 06:34:40

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.

在大多数Unix和Posix符合操作系统下,使用O_APPEND执行open()操作系统调用向OS指示写入是原子追加和写入操作。出现这种情况,对于本地文件系统,当您执行写操作时,您知道它会附加到文件的末尾。

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

Windows操作系统通过将适当参数中的FILE_APPEND_DATA传递给Win32 CreateFile()系统调用来支持相同的功能。

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.

我的问题是,我无法确定如何使用Net Framework库在C#下获得此行为,有没有办法使用Net Framework获取此类行为?顺便说一句,我不相信使用FileMode.Append会给出这样的行为。

3 个解决方案

#1


31  

Use one of the overloads of the FileStream constructor:

使用FileStream构造函数的一个重载:

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

FileSystemRights.AppendData corresponds with FILE_APPEND_DATA

FileSystemRights.AppendData与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.

FileStream似乎坚持缓冲,因此请确保每次写入时缓冲区足够大,并在每次写入后调用Flush()。

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);
            }
        }
    }

#2


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.

您可以使用带有所需参数的PInvoke调用CreateFile,并将生成的句柄传递给其中一个FileFream构造函数,该构造函数将SafeFileHandle作为参数。

#3


-1  

Why can't you use

你为什么不能用

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

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

?这也是一个线程安全/“原子”调用。

#1


31  

Use one of the overloads of the FileStream constructor:

使用FileStream构造函数的一个重载:

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

FileSystemRights.AppendData corresponds with FILE_APPEND_DATA

FileSystemRights.AppendData与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.

FileStream似乎坚持缓冲,因此请确保每次写入时缓冲区足够大,并在每次写入后调用Flush()。

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);
            }
        }
    }

#2


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.

您可以使用带有所需参数的PInvoke调用CreateFile,并将生成的句柄传递给其中一个FileFream构造函数,该构造函数将SafeFileHandle作为参数。

#3


-1  

Why can't you use

你为什么不能用

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

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

?这也是一个线程安全/“原子”调用。