在大多数 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.
发布评论
评论(3)
使用
FileStream
构造函数的重载之一:FileSystemRights.AppendData
对应于FILE_APPEND_DATA
FileStream 似乎坚持缓冲,因此请确保缓冲区是每个都足够大
每次写入后写入并调用
Flush()
。小例子:
Use one of the overloads of the
FileStream
constructor:FileSystemRights.AppendData
corresponds withFILE_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:
您可以使用 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.
为什么你不能使用
?这也是线程安全/“原子”调用。
Why can't you use
? That's a thread-safe/"atomic" call as well.