0

The sample I want to use is from This SO article.

public static bool HasWritePermissionOnDir(string path)
{
    var writeAllow = false;
    var writeDeny = false;
    var accessControlList = Directory.GetAccessControl(path);
    if (accessControlList == null)
        return false;
    var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
    if (accessRules == null)
        return false;

    foreach (FileSystemAccessRule rule in accessRules)
    {
        if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;

        if (rule.AccessControlType == AccessControlType.Allow)
            writeAllow = true;
        else if (rule.AccessControlType == AccessControlType.Deny)
            writeDeny = true;
    }

    return writeAllow && !writeDeny;
}

How can I use the "return writeAllow && !writeDeny" in an If statement? Or can I even do that. Sorry if this is a dumb question, but in my limited experience I haven't see a return look like that.

2
  • 1
    You haven't seen the keyword return followed by an expression? Does return 1 + 2; look any more familiar? It's the same thing. Commented Feb 14, 2012 at 22:18
  • or better yet. return true && false; Commented Feb 14, 2012 at 22:20

5 Answers 5

3

Is this what you mean by "using it in an if-statement"??

if (HasWritePermissionOnDir(mypath))
{
    // Then write a new file to the directory
}

This code calls the function with mypath, and based on the returned result (which boils down to writeAllow && !writeDeny), it may or may not evaluate to true and execute the if-statement.

1
  • Thanks everyone. I was missing the (mypath) part.
    – JimDel
    Commented Feb 14, 2012 at 22:43
2

writeAllow && !writeDeny eavluates to a bool. The only requirement for an if statement is that whatever is inside the parentheses evaluates to a bool. So using the expression inside of an if statement is as simple as:

if (writeAllow && !writeDeny)
{
    //do something
}
2

The && operator is a boolean operator, meaning it returns either true or false. That means you can use this expression in an if statement exactly the way it is:

if (writeAllow && !writeDeny)

will work.

2

If I understand, you want to take this code and put it inside one of your own functions. Is that correct?

Firstly, I suggest you leave it as a separate function. It makes it easier to discern when that piece of code is meant to do. You can use it in an if statement with:

if (HasWritePermissionOnDir("C:\\My\\Path"))
{
    // TODO
}

Or if you insist on copying the code inline:

// TODO: Content of HasWritePermissionOnDir method without the return statement...
if (WriteAllow && !writeDeny)
{
    // TODO
}
2

At the end of the method, the boolean value is evaluated and returned in one line. But you could rewrite it like this (not recommended, just showing for clarity):

bool ValueToReturn = WriteAllow && !writeDeny;
return ValueToReturn;

Or if you think about it in plain English:

Return true if write is allowed and not denied.

The double ampersand (&) is a logical short-circuit meaning (in this case) that if the first term evaluates to false, it won't matter if the second term does or not, so it won't be evaluated.

3
  • If I ran across this code the first thing I would do is refactor the ValueToReturn variable out. I wouldn't consider it as being "rewritten in plain english". Commented Feb 14, 2012 at 22:22
  • 1
    I would refactor it out too. I was trying to show how the return statement evaluates the variables and returns the result. (In case the OP was confused about how that worked. It's not entirely clear what the question is asking.)
    – JYelton
    Commented Feb 14, 2012 at 22:24
  • Very true. The question is a bit cryptic Commented Feb 14, 2012 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.