System.IO.FileStream FileAccess与FileShare [英] System.IO.FileStream FileAccess vs FileShare

查看:182
本文介绍了System.IO.FileStream FileAccess与FileShare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了所有内容,但找不到该问题的答案.我了解文件共享处理共享,但是我找不到关于共享如何精确地以及它们如何相互影响的解释.

I've searched all over but can't find an answer to this question. I understand that FileAccess deals with file access on the machine and FileShare deals with the share, but I can't find an explanation of how exactly it comes together and how they impact one another.

例如,如果我有

using ( FileStream fs = new FileStream( pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) )

这是否意味着计算机上的用户只能读取文件,而远程访问文件夹的用户可以读取和写入文件?另外,使用会产生什么影响

does that mean that users on the machine can only read the file whilst users remotely accessing the folder can read and write to the file? Additionally, what would the impact be in using

using ( FileStream fs = new FileStream( pathName, FileMode.Open, FileAccess.Read ) )

我未指定FileShare的地方吗?

Where I haven't specified the FileShare?

推荐答案

FileAccess会说明您将如何处理文件.很容易理解,您将知道自己将要读或写.

FileAccess says what you are going to do with the file. Pretty easy to understand, you'll know you are going to read or write.

FileShare非常棘手,因为它需要您踏入另一位程序员的脚.它确定如果另一个进程也打开了文件,该怎么办.访问文件的两个进程可能非常麻烦,您需要通过可能的故障模式进行推理.您选择的值与文件类型和所需的访问权限密切相关.按您要做什么将其分解:

FileShare is the much trickier one since it requires you to step into the shoes of another programmer. It determines what another process can do if it also has the file opened. Two processes accessing a file can be very troublesome, you'll need to reason through the possible failure modes. The value you pick is strongly correlated to the type of the file and the access you want. Breaking it down by what you are going to do:

FileAccess.Read

FileAccess.Read

如果另一个进程也从文件中读取,则永远不会有任何麻烦.因此,FileShare.Read是默认选择.

There is never any trouble if another process also reads from the file. So FileShare.Read is the default choice.

如果另一个进程已经打开文件进行写入,则可能需要FileShare.ReadWrite.它已经获得了写访问权,因此您永远无法仅使用FileShare.Read来自己打开文件,您不能拒绝写入,因为首先是其他过程,您将被拒绝访问.通常,这仅对文本文件有用,在这种情况下,您可以确保其他过程仅将文本附加到文件末尾.日志文件是非常常见的情况.仍然可能很棘手,当进程将更改刷新到文件时,这确实很重要.您可能会观察到部分书写的文本行,请注意这一点.

You may need FileShare.ReadWrite if another process has already opened the file for writing. It already gained write access so you can never open the file yourself with just FileShare.Read, you can't deny writing since that other process was first, you'll be denied access instead. This generally only comes to a good on a text file, the kind where you can be sure that the other process is only ever appending text to the end of the file. A log file is the very common scenario. Still possibly tricky, it matters exactly when that process flushes changes to the file. You might observe partially written lines of text, beware of this.

FileAccess.Write

FileAccess.Write

您不能使用FileShare.Write或FileShare.ReadWrite.由于这将允许两个进程同时写入文件,因此文件内容将是两个程序输出的混杂.可能的解决方法是这些进程仲裁对文件的访问,以确保其中只有一个可以同时访问文件.通常由一个命名的互斥体实现.

You cannot use FileShare.Write or FileShare.ReadWrite. Since that would allow two processes writing to the file at the same time, the file content will be a jumble of output from both programs. A possible workaround is these processes arbitrating access to the file, ensuring only one of them can ever access the file at the same time. Normally implemented by a named mutex.

您可以使用FileShare.Read(如果它是文本文件),与上文所述的日志文件相同.否则,默认选择应为FileShare.None

You can use FileShare.Read if it is text file, same scenario I described above with the log file. The default choice otherwise should be FileShare.None

FileAcces.ReadWrite

FileAcces.ReadWrite

不太常见,仅在写入二进制数据并使用Seek()时使用.在执行此操作的过程中,没有其他任何进程能够正确读取文件的希望,假设它们自己不仲裁访问,则必须使用FileShare.None.

Not that common, only used if you write binary data and use Seek(). There is no hope that any other process could ever read the file correctly while you are doing this, assuming they don't arbitrate access themselves, you must use FileShare.None.

这篇关于System.IO.FileStream FileAccess与FileShare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆