码迷,mamicode.com
首页 > 系统相关 > 详细

FileStream: 文件“XXX”正由另一进程使用,因此该进程无法访问此文件

时间:2020-04-29 18:02:27      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:readwrite   users   打开   mamicode   ons   access   mes   共享   code   

1.现象说明(仅测试FileShare=ReadWrite时候的影响,FileAccess保持一致, 如果FileAccess不一致的情况,另论,此处不讨论):

a.  FileAccess=Read, FileShare=ReadWrite未释放之后再打开文件:

FileStream file;
file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (Exception ex)
{
    Console.WriteLine($"None:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception ex)
{
    Console.WriteLine($"Read:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Read, FileShare.Write);
}
catch (Exception ex)
{
    Console.WriteLine($"Write:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (Exception ex)
{
    Console.WriteLine($"ReadWrite:   {ex.Message}");
}

 

异常信息如下:

技术图片

 

b. 调整 FileAccess=Write, FileShare=ReadWrite未释放之后再打开文件:

FileStream file;
file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Write, FileShare.None);
}
catch (Exception ex)
{
    Console.WriteLine($"None:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Write, FileShare.Read);
}
catch (Exception ex)
{
    Console.WriteLine($"Read:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Write, FileShare.Write);
}
catch (Exception ex)
{
    Console.WriteLine($"Write:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
}
catch (Exception ex)
{
    Console.WriteLine($"ReadWrite:   {ex.Message}");
}

 

异常信息如下:

 技术图片

 

 c.  调整为FileAccess=ReadWrite, FileShare=ReadWrite未释放之后再打开文件:

FileStream file;
file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (Exception ex)
{
    Console.WriteLine($"None:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
}
catch (Exception ex)
{
    Console.WriteLine($"Read:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Write);
}
catch (Exception ex)
{
    Console.WriteLine($"Write:   {ex.Message}");
}

try
{
    file = new FileStream(@"C:\Users\croot\Desktop\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
}
catch (Exception ex)
{
    Console.WriteLine($"ReadWrite:   {ex.Message}");
}

异常信息如下:

技术图片

 

2. 加上file.Close()则正常,

FileAccess:

技术图片

 

 FileShare:

技术图片

 

通过官网看,微软说的并不详细,应该是即使用了FileShare.ReadWrite, 那么如果在文件未关闭的时候,后续操作文件的FileShare也必须和上次未关闭的流式的FileAccess或者FileShare一致的才行

结论:FileShare不只是对随后的打开文件请求有影响,事实是已经打开的未释放的文件句柄的访问或者共享模式对随后打开的文件请求也有影响

参考:

https://www.cnblogs.com/mschen/p/5353862.html

https://www.cnblogs.com/feiyuhuo/p/5414788.html

 

FileStream: 文件“XXX”正由另一进程使用,因此该进程无法访问此文件

标签:readwrite   users   打开   mamicode   ons   access   mes   共享   code   

原文地址:https://www.cnblogs.com/lcawen/p/12803433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!