Programmermid
Programmermid

Reputation: 598

Not all Code Path returns a value

I have a method which is checking for write access of a folder. But its giving me error saying not all code path returns a value?

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;
    }
}

what am I missing in this method ?

Upvotes: 1

Views: 153

Answers (3)

Magnus Alexander
Magnus Alexander

Reputation: 96

I'll recommend this for your method, because you're not returning a boolean on all ways:

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;
}

Upvotes: 1

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

You're only ever returning a value when an exception is caught. That's why your compiler is telling you that.

Upvotes: 2

Neil N
Neil N

Reputation: 25258

If an error isn't thrown, no boolean gets returned.

You need a return true/false at the end, after the try/catch.

NOT throwing an error is a possible "code path" which the compiler needs a return type for.

Upvotes: 4

Related Questions