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

发布于 2024-08-13 17:59:32 字数 637 浏览 1 评论 0 原文

在大多数 Unix 和 Posix 兼容操作系统下,使用 O_APPEND 执行 open() 操作系统调用会向操作系统指示写入操作是原子追加和写入操作。通过这种行为,对于本地文件系统,当您进行写入时,您知道它会被附加到文件末尾。

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

参考文献:

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

我的问题是这样的,我无法确定如何使用 Net Framework 在 C# 下获得此行为 图书馆,有没有办法使用网络框架获得这样的行为? 顺便说一句,我不相信使用 FileMode.Append 会产生这种行为。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-08-20 17:59:32

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

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

FileSystemRights.AppendData 对应于 FILE_APPEND_DATA

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

小例子:

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

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);
            }
        }
    }
情愿 2024-08-20 17:59:32

您可以使用 CreateFile "http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html" rel="nofollow">PInvoke 以及所需的参数,并将生成的句柄传递给 FileStream 构造函数 将 SafeFileHandle 作为参数。

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.

热血少△年 2024-08-20 17:59:32

为什么你不能使用

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

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

Why can't you use

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文