• 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

  • 相关阅读:
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Software Development Engineer
    微软职位内部推荐-Senior Dev Lead
    微软职位内部推荐-Software Engineer II_HPC
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior SW Engineer for Application Ecosystem
    微软职位内部推荐-Senior Software Engineer-Eco
    微软职位内部推荐-Software Development Engineer II
    微软职位内部推荐-Senior Software Lead-Index Gen
  • 原文地址:https://www.cnblogs.com/lcawen/p/12803433.html
Copyright © 2020-2023  润新知