标签: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