【发布时间】:2015-08-28 06:05:29
【问题描述】:
当使用文件流时,将FileShare 设置为None,并假设两个用户同时访问同一个函数想要读/写该文件。 FileShare.None 会让第二个用户请求等待还是第二个用户的请求会抛出异常?
//two users get to this this code at the same time
using (FileStream filestream = new FileStream(chosenFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
using (StreamReader sr = new StreamReader(filestream))
using (StreamWriter sw = new StreamWriter(filestream))
{
//reading and writing to file
}
Msdn 说:无 拒绝共享当前文件。任何打开文件的请求(通过此进程或其他进程)都将失败,直到文件关闭。
但是请求会一直尝试直到文件流关闭吗?
【问题讨论】:
标签: c# .net file thread-safety fileshare