Tags: file, microsoft, msdn, particular, permissions, programmatically, software, visual

Get file permissions

On Microsoft » Microsoft Visual C#

5,610 words with 2 Comments; publish: Thu, 20 Dec 2007 21:09:00 GMT; (30047.12, « »)

How can I programmatically find the permissions of a particular file in Csharp?

All Comments

Leave a comment...

  • 2 Comments
    • Hi


      using System.Security.AccessControl;

      private void _btnCheck_Permissions_Clicked(object sender, EventArgs e)

      {

      DirectorySecurity ds = new DirectorySecurity(.visual-csharp.todaysummary.com."D:\Test", AccessControlSections.All);

      try

      {

      Type t = Type.GetType("System.Security.Principal.SecurityIdentifier");

      ir = ds.GetOwner(t);

      Console.WriteLine("Owner: {0}", ir);

      arc = ds.GetAccessRules(true, true, t);

      if (arc != null)

      {

      foreach (AuthorizationRule ar in arc)

      {

      AccessRule aru = (AccessRule)ar;

      Console.WriteLine("ID={0}, AccessType={1}", ar.IdentityReference, aru.AccessControlType);

      }

      }

      }

      catch (Exception ex)

      {

      Console.WriteLine(ex.Message);

      Console.WriteLine(ex.StackTrace);

      }

      }


      Hope this helps.

      #1; Mon, 10 Sep 2007 16:32:00 GMT
    • that looks like it Sibusiso, what I want to do is get the file permissions, change the file permissions and then revert back to the original file permissions.

      Thanks.

      #2; Mon, 10 Sep 2007 16:33:00 GMT