Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并非所有代码路径都返回值_C# - Fatal编程技术网

C# 并非所有代码路径都返回值

C# 并非所有代码路径都返回值,c#,C#,我有一个方法,检查文件夹的写访问权限。但它给我的错误是不是所有的代码路径都返回一个值 public bool AccessPackerPlanTemplate(string folderPath) { try { string path = @"\\Sample"; string NtAccountName = @"Sample"; DirectoryInfo di = new DirectoryInfo(pa

我有一个方法,检查文件夹的写访问权限。但它给我的错误是不是所有的代码路径都返回一个值

public bool AccessPackerPlanTemplate(string folderPath)
{          
    try
    {
        string path = @"\\Sample";
        string NtAccountName = @"Sample";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
        AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));

        //Go through the rules returned from the DirectorySecurity
        foreach (AuthorizationRule rule in rules)
        {
            //If we find one that matches the identity we are looking for
            if (rule.IdentityReference.Value.Equals(NtAccountName, StringComparison.CurrentCultureIgnoreCase))
            {
                //Cast to a FileSystemAccessRule to check for access rights
                if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData) > 0)
                {
                    //Show the link
                }
            }
        }
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

这个方法缺少什么?

如果没有抛出错误,则不会返回布尔值

在try/catch之后,您需要在末尾返回true/false


不抛出错误是编译器需要返回类型的可能代码路径。

如果未抛出错误,则不会返回布尔值

在try/catch之后,您需要在末尾返回true/false


不抛出错误可能是编译器需要返回类型的代码路径。

只有在捕获异常时才返回值。这就是编译器告诉您的原因。

只有在捕获异常时才返回值。这就是为什么您的编译器会告诉您这一点。

我建议您的方法使用这一点,因为您并不是在所有方面都返回布尔值:

public bool AccessPackerPlanTemplate(string folderPath)
{    
    bool result = false;      
    try
    {
         string path = @"\\Sample";
         string NtAccountName = @"Sample";
         //... Your code
         if(/*Your Condition*/)
         {
             result = true;
         }
    }
    catch (UnauthorizedAccessException)
    {
       result = false;
    }
    return result;
}

我将为您的方法推荐此选项,因为您并不是在所有方面都返回布尔值:

public bool AccessPackerPlanTemplate(string folderPath)
{    
    bool result = false;      
    try
    {
         string path = @"\\Sample";
         string NtAccountName = @"Sample";
         //... Your code
         if(/*Your Condition*/)
         {
             result = true;
         }
    }
    catch (UnauthorizedAccessException)
    {
       result = false;
    }
    return result;
}

如果没有UnauthorizedAccessException,方法的返回值应该是什么?显示链接做什么?除非得到异常,否则不会返回值。如果没有UnauthorizedAccessException,方法的返回值应该是什么?显示链接做什么?除非得到异常,否则永远不会返回值。