Check file permission in (local/network share) directory? RRS feed

  • Question

  • Hi,

    I need to check whether a user has read/write permission to a file on any directories (local or network share).

    Can someone provide a short code sample?

    I've seen many examples online (pinvoking NetShareGetInfo, DirectoryInfo.GetAccessControl(), FileIOPermission.Demand(), etc), but I'm still unable to correctly check if a file in a network share directory (e.g, \\networkPC\sharedFolder) is readable/writable.

    All my checks will pass, but when I attempt to write a file, I get UnauthorizedAccessException ("Access to the path ... is denied").

    Should I really be doing my checks this way?

    public static bool IsFileWritable(string fileName)
      bool isWritable;
      FileInfo file = new FileInfo(fileName);
        using (Stream stream = file.OpenWrite()) // attempt to open a write stream.
        isWritable = true;
        isWritable = false;
      return isWritable;

    • Moved by SamAgain Friday, November 5, 2010 10:28 AM not CLR question (From:Common Language Runtime)
    Friday, October 29, 2010 4:56 AM