简体   繁体   English

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

[英]Handle Unauthorized Access Before File Not Found

When someone wants to access a file in a shared folder, two issues might happen: first is that the user does not have permission to access a folder or file and second the file is not there! 当某人想要访问共享文件夹中的文件时,可能会发生两个问题:首先是用户无权访问文件夹或文件,其次文件不存在! But I'm facing a problem in case of the user doesn't have access to the folder and the file is not in that folder. 但是,如果用户无权访问该文件夹并且该文件不在该文件夹中,则我将面临一个问题。 In this case, it has thrown a FileNotFoundException instead of UnauthorizedAccessException ! 在这种情况下,它抛出了FileNotFoundException而不是UnauthorizedAccessException
Below I wrote a bunch of code to handle these issues. 下面,我编写了一些代码来处理这些问题。
Solution 1: Use File.Exist 解决方案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;
        }
    }

Output 1 输出1
在此处输入图片说明

Solution 2: 解决方案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;
        }
    }

Output 2 输出2
在此处输入图片说明

So the main problem is that if a user doesn't have access to a specific folder and even the file is not there why it doesn't show Access Denied instead of File Not Found? 因此,主要问题是,如果用户无权访问特定文件夹,甚至文件都不存在,为什么它不显示“拒绝访问”而不是“找不到文件”?
Thank you for your time :) 感谢您的时间 :)

According to the documentation: UnauthorizedAccessException path specified a file that is read-only. 根据文档:UnauthorizedAccessException 路径指定了一个只读文件。

It makes me think that the method try to read the file (as path is correct), however it doesn't find the file, so it can't say that if file is read-only or not. 它使我认为该方法尝试读取文件(因为路径正确),但是找不到该文件,因此不能说文件是否为只读。 Due to this it actually sends a file not found exception rather than the expected exception. 因此,它实际上发送的是文件未找到异常,而不是预期的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM