Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File Permission

File Permission

Scheduled Pinned Locked Moved C#
jsonhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarkSorrow38
    wrote on last edited by
    #1

    I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.

    using System;
    using System.IO;
    public class RegistryEditor
    {
    public static void Main()
    {
    try
    {
    FileInfo f = new FileInfo(@"C:\Test.txt");
    f.Attributes = FileAttributes.System;
    FileInfo f1 = new FileInfo(@"C:\Test.txt");
    f1.Attributes = FileAttributes.ReadOnly;
    FileInfo f2 = new FileInfo(@"C:\Test.txt");
    f2.Attributes = FileAttributes.Hidden;
    FileInfo i = new FileInfo(@"C:\Test1.txt");
    i.Attributes = FileAttributes.System;
    i.Attributes = FileAttributes.Hidden;
    i.Attributes = FileAttributes.ReadOnly;
    }
    catch (Exception ex)
    {
    Console.Write(ex.ToString());
    }
    }
    }

    Banking establishments are more dangerous then standing armies.

    L OriginalGriffO 2 Replies Last reply
    0
    • D DarkSorrow38

      I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.

      using System;
      using System.IO;
      public class RegistryEditor
      {
      public static void Main()
      {
      try
      {
      FileInfo f = new FileInfo(@"C:\Test.txt");
      f.Attributes = FileAttributes.System;
      FileInfo f1 = new FileInfo(@"C:\Test.txt");
      f1.Attributes = FileAttributes.ReadOnly;
      FileInfo f2 = new FileInfo(@"C:\Test.txt");
      f2.Attributes = FileAttributes.Hidden;
      FileInfo i = new FileInfo(@"C:\Test1.txt");
      i.Attributes = FileAttributes.System;
      i.Attributes = FileAttributes.Hidden;
      i.Attributes = FileAttributes.ReadOnly;
      }
      catch (Exception ex)
      {
      Console.Write(ex.ToString());
      }
      }
      }

      Banking establishments are more dangerous then standing armies.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi,

      int i;
      i=1;
      i=2;
      i=4;

      Now what is the final value of i? Is it 7? Why would it be any different with file attributes? File attributes are flags, i.e. they have a binary pattern with one bit set, all others cleared. So you can OR them together (with the '|' operator) to get combinations of them. :)

      Luc Pattyn


      Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


      Local announcement (Antwerp region): Lange Wapper? Neen!


      1 Reply Last reply
      0
      • D DarkSorrow38

        I was just trying to give file premission to my dummy files but encountered a problem. I found out that only the last applied permission is imposed on the file while rest are overriden or not applied.

        using System;
        using System.IO;
        public class RegistryEditor
        {
        public static void Main()
        {
        try
        {
        FileInfo f = new FileInfo(@"C:\Test.txt");
        f.Attributes = FileAttributes.System;
        FileInfo f1 = new FileInfo(@"C:\Test.txt");
        f1.Attributes = FileAttributes.ReadOnly;
        FileInfo f2 = new FileInfo(@"C:\Test.txt");
        f2.Attributes = FileAttributes.Hidden;
        FileInfo i = new FileInfo(@"C:\Test1.txt");
        i.Attributes = FileAttributes.System;
        i.Attributes = FileAttributes.Hidden;
        i.Attributes = FileAttributes.ReadOnly;
        }
        catch (Exception ex)
        {
        Console.Write(ex.ToString());
        }
        }
        }

        Banking establishments are more dangerous then standing armies.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        You are going to kick yourself! You are, you are...

        Ishaan Karnik wrote:

                FileInfo f = new FileInfo(@"C:\\Test.txt");
                f.Attributes = FileAttributes.System;
        

        Assume it works: the attributes for the file are now "System"

        Ishaan Karnik wrote:

                FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                f1.Attributes = FileAttributes.ReadOnly;
        

        Assume it works: the attributes for the file are now "ReadOnly" Had you considered?

                FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                f1.Attributes += FileAttributes.ReadOnly;
        

        What are the attributes now? I told you, you would kick yourself!

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L D 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          You are going to kick yourself! You are, you are...

          Ishaan Karnik wrote:

                  FileInfo f = new FileInfo(@"C:\\Test.txt");
                  f.Attributes = FileAttributes.System;
          

          Assume it works: the attributes for the file are now "System"

          Ishaan Karnik wrote:

                  FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                  f1.Attributes = FileAttributes.ReadOnly;
          

          Assume it works: the attributes for the file are now "ReadOnly" Had you considered?

                  FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                  f1.Attributes += FileAttributes.ReadOnly;
          

          What are the attributes now? I told you, you would kick yourself!

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Except that if it already was ReadOnly, it will be Hidden and not ReadOnly anymore. (ReadOnly = 1, Hidden = 2)

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            Except that if it already was ReadOnly, it will be Hidden and not ReadOnly anymore. (ReadOnly = 1, Hidden = 2)

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Yes you are right, I'll start kicking myself when I stop typing: it should be "|=" not "+=" :doh:

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              You are going to kick yourself! You are, you are...

              Ishaan Karnik wrote:

                      FileInfo f = new FileInfo(@"C:\\Test.txt");
                      f.Attributes = FileAttributes.System;
              

              Assume it works: the attributes for the file are now "System"

              Ishaan Karnik wrote:

                      FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                      f1.Attributes = FileAttributes.ReadOnly;
              

              Assume it works: the attributes for the file are now "ReadOnly" Had you considered?

                      FileInfo f1 = new FileInfo(@"C:\\Test.txt");
                      f1.Attributes += FileAttributes.ReadOnly;
              

              What are the attributes now? I told you, you would kick yourself!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              D Offline
              D Offline
              DarkSorrow38
              wrote on last edited by
              #6

              OriginalGriff, Yes, i had considered FileInfo i = new FileInfo(@"C:\Test.txt"); i.Attributes += FileAttributes.ReadOnly; But this gives an error Operator ' += ' cannot be applied to operands of type 'System.IO.FileAttributes' and 'System.IO.FileAttributes'. Luc Pattyn, ThankYou. Your suggestion worked.

              L 1 Reply Last reply
              0
              • D DarkSorrow38

                OriginalGriff, Yes, i had considered FileInfo i = new FileInfo(@"C:\Test.txt"); i.Attributes += FileAttributes.ReadOnly; But this gives an error Operator ' += ' cannot be applied to operands of type 'System.IO.FileAttributes' and 'System.IO.FileAttributes'. Luc Pattyn, ThankYou. Your suggestion worked.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                you're welcome. :)

                Luc Pattyn


                Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


                Local announcement (Antwerp region): Lange Wapper? Neen!


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups