在C#中,如果有2个进程正在读取和写入同一个文件,避免进程锁定异常的最好方法是什么? [英] In C#, if 2 processes are reading and writing to the same file, what is the best way to avoid process locking exceptions?

查看:1066
本文介绍了在C#中,如果有2个进程正在读取和写入同一个文件,避免进程锁定异常的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的文件读取代码:使用(FileStream fileStream = new FileStream(fileName,FileMode.Open,FileAccess)创建一个新的文件。 Read,FileShare.None))
{
using(TextReader tr = new StreamReader(fileStream))
{
string fileContents = tr.ReadToEnd();


$ / code $ / pre

和下面的文件写入代码:

使用(TextWriter tw = new StreamWriter(fileName))
{
tw.Write(fileContents);
$ b

  
tw.Close();



$ b $ p
$ b

以下例外情况可见:


进程无法访问文件
'c:\temp\myfile.txt',因为它是另一个进程使用的
。 p>

避免这种情况的最好方法是什么?阅读器是否需要在收到异常时重试,或者是否有更好的方法?

请注意,阅读器进程正在使用 FileSystemWatcher 知道文件何时发生变化。

另外请注意,在这种情况下,我不是寻找替代方法来共享字符串2进程。

解决方案

您可以打开一个文件进行写入,只锁定写入权限,从而允许其他人读取文件。例如,

 使用(FileStream stream = new FileStream( @C:\Myfile.txt,FileMode.Open,FileAccess.ReadWrite,FileShare.Read))
{
//在这里写你的文字。





$ b

其他文件访问只是打开文件读取而不写入,并允许读写共享。

 使用(FileStream stream = new FileStream(@C:\Myfile.txt,FileMode.Open,FileAccess .Read,FileShare.ReadWrite))
{
//在这里阅读。



$ b $ p
$ b如果你想确保读者总是阅读最新的文件,您需要使用一个锁定文件,指出有人正在写入文件(尽管如果不仔细实施,可能会遇到竞争状态),或者确保在打开阅读和处理异常时阻止写入共享可以再试一次,直到获得独家使用权。


With the following file reading code:

using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
    using (TextReader tr = new StreamReader(fileStream))
    {
        string fileContents = tr.ReadToEnd();
    }
}

And the following file write code:

using (TextWriter tw = new StreamWriter(fileName))
{
    tw.Write(fileContents);
    tw.Close();
}

The following exception details are seen:

The process cannot access the file 'c:\temp\myfile.txt' because it is being used by another process.

What is the best way of avoiding this? Does the reader need to retry upon receipt of the exception or is there some better way?

Note that the reader process is using a FileSystemWatcher to know when the file has changed.

Also note that, in this instance, I'm not looking for alternatives ways of sharing strings between the 2 processes.

解决方案

You can open a file for writing and only lock write access, thereby allowing others to still read the file.

For example,

using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
   // Do your writing here.
}

Other file access just opens the file for reading and not writing, and allows readwrite sharing.

using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
   // Does reading  here.
}

If you want to ensure that readers will always read an up-to-date file, you will either need to use a locking file that indicates someone is writing to the file (though you may get a race condition if not carefully implemented) or make sure you block write-sharing when opening to read and handle the exception so you can try again until you get exclusive access.

这篇关于在C#中,如果有2个进程正在读取和写入同一个文件,避免进程锁定异常的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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