FileStream: 文件“XXX”正由另一进程使用,因此该进程无法访问此文件
Posted lcawen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FileStream: 文件“XXX”正由另一进程使用,因此该进程无法访问此文件相关的知识,希望对你有一定的参考价值。
1.现象说明(仅测试FileShare=ReadWrite时候的影响,FileAccess保持一致, 如果FileAccess不一致的情况,另论,此处不讨论):
a. FileAccess=Read, FileShare=ReadWrite未释放之后再打开文件:
FileStream file; file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Read, FileShare.None); } catch (Exception ex) { Console.WriteLine($"None: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception ex) { Console.WriteLine($"Read: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Read, FileShare.Write); } catch (Exception ex) { Console.WriteLine($"Write: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.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:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Write, FileShare.ReadWrite); try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Write, FileShare.None); } catch (Exception ex) { Console.WriteLine($"None: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Write, FileShare.Read); } catch (Exception ex) { Console.WriteLine($"Read: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.Write, FileShare.Write); } catch (Exception ex) { Console.WriteLine($"Write: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.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:UserscrootDesktop1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (Exception ex) { Console.WriteLine($"None: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read); } catch (Exception ex) { Console.WriteLine($"Read: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Write); } catch (Exception ex) { Console.WriteLine($"Write: {ex.Message}"); } try { file = new FileStream(@"C:UserscrootDesktop1.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”正由另一进程使用,因此该进程无法访问此文件的主要内容,如果未能解决你的问题,请参考以下文章
c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件
c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件
Winform 拷贝文件到PDA,提示:正由另一进程使时用,因此该进程无法访问该文件。