找不到文件之前处理未经授权的访问

时间:2019-02-14 11:08:12

标签: c#

当某人想要访问共享文件夹中的文件时,可能会发生两个问题: 首先是用户无权访问文件夹或文件,其次文件不存在!但是,如果用户无权访问该文件夹并且该文件不在该文件夹中,则我将面临一个问题。在这种情况下,它抛出了FileNotFoundException而不是UnauthorizedAccessException
下面,我写了一堆代码来处理这些问题。
解决方案1:使用File.Exist

    static void Main(string[] args)
    {
        NetworkCredential nc = new NetworkCredential("Administartor", "xxx", "xxx");
        CredentialCache cc = new CredentialCache();
        cc.Add(new Uri(@"\\WIN-xxx"), "Basic", nc);
        var address_access_exist = @"\\WIN-xxx\Z-Test-A-E\file.txt";
        var address_access_not_exist = @"\\WIN-xxx\Z-Test-A-NE\file.txt";
        var address_not_access_exist = @"\\WIN-xxx\Z-Test-NA-E\file.txt";
        var address_not_access_not_exist = @"\\WIN-xxx\Z-Test-NA-NE\file.txt";
        Console.WriteLine(check(address_access_exist));
        Console.WriteLine(check(address_access_not_exist));
        Console.WriteLine(check(address_not_access_exist));
        Console.WriteLine(check(address_not_access_not_exist));
        Console.ReadLine();
    }

    public static string check(string address)
    {
        try
        {
            if (File.Exists(address))
            {
                return File.ReadAllText(address);
            }
            else
            {
                return "Not Exist!";
            }
        }
        catch (UnauthorizedAccessException e)
        {
            return e.Message;
        }
        catch (FileNotFoundException e)
        {
            return e.Message;
        }
    }

输出1
enter image description here

解决方案2:

    static void Main(string[] args)
    {
        NetworkCredential nc = new NetworkCredential("Administartor", "xxx", "xxx");
        CredentialCache cc = new CredentialCache();
        cc.Add(new Uri(@"\\WIN-xxx"), "Basic", nc);
        var address_access_exist = @"\\WIN-xxx\Z-Test-A-E\file.txt";
        var address_access_not_exist = @"\\WIN-xxx\Z-Test-A-NE\file.txt";
        var address_not_access_exist = @"\\WIN-xxx\Z-Test-NA-E\file.txt";
        var address_not_access_not_exist = @"\\WIN-xxx\Z-Test-NA-NE\file.txt";
        Console.WriteLine(check(address_access_exist));
        Console.WriteLine(check(address_access_not_exist));
        Console.WriteLine(check(address_not_access_exist));
        Console.WriteLine(check(address_not_access_not_exist));
        Console.ReadLine();
    }

    public static string check(string address)
    {
        try
        {
            if (File.ReadAllText(address).Length >= 0)
            {
                return File.ReadAllText(address);
            }
            else
            {
                return "Not Exist!";
            }
        }
        catch (UnauthorizedAccessException e)
        {
            return e.Message;
        }
        catch (FileNotFoundException e)
        {
            return e.Message;
        }
    }

输出2
enter image description here

因此,主要问题是,如果用户无权访问特定文件夹,甚至文件也没有,为什么它不显示“拒绝访问”而不是“找不到文件”?
谢谢您的时间:)

1 个答案:

答案 0 :(得分:0)

根据文档: UnauthorizedAccessException 路径指定了一个只读文件。

这让我认为该方法尝试读取文件(因为路径正确),但是找不到该文件,因此不能说文件是否为只读。因此,它实际上发送的是文件未找到异常,而不是预期的异常。