Reputation: 598
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
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
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
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