Setting some file attributes doesn't work with FileInfo.Attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John R.

    #1

    Setting some file attributes doesn't work with FileInfo.Attributes

    How do you set the following file attributes:

    Compressed
    Encrypted
    Normal
    ReparsePoint
    SparsePoint

    You CAN'T set these using FileInfo.Attrib utes or File.SetAttribu tes. It
    doesn't work for these file attributes.

    Please show me some code how to set those attributes. If I have to use a
    Win 32 API call then please show me how to do it.

    I found you can set Archive, Hidden, NotContentIndex ed, Offline, ReadOnly,
    System, and Temporary using either FileInfo.Attrib utes or File.SetAttribu tes.

    I just need a method that works to set the remaining file attributes.

    Thanks,

    -- John...

  • Marcin GrzÄ™bski

    #2
    Re: Setting some file attributes doesn't work with FileInfo.Attrib utes

    Hi John,

    Are you sure that your file system supports these flags?
    I think that they might be something like "reserved" or
    "temporaly not used".
    I know that its not an answer, but maybe you want to
    fight with the windmills.

    Marcin
    [color=blue]
    > How do you set the following file attributes:
    >
    > Compressed
    > Encrypted
    > Normal
    > ReparsePoint
    > SparsePoint
    >
    > You CAN'T set these using FileInfo.Attrib utes or File.SetAttribu tes. It
    > doesn't work for these file attributes.
    >
    > Please show me some code how to set those attributes. If I have to use a
    > Win 32 API call then please show me how to do it.
    >
    > I found you can set Archive, Hidden, NotContentIndex ed, Offline, ReadOnly,
    > System, and Temporary using either FileInfo.Attrib utes or File.SetAttribu tes.
    >
    > I just need a method that works to set the remaining file attributes.
    >
    > Thanks,
    >
    > -- John...
    >[/color]

    Comment

    • John R.

      #3
      Re: Setting some file attributes doesn't work with FileInfo.Attrib

      Take a look at this:

      File.SetAttribu tes Method
      Sets the specified FileAttributes of the file on the specified path.

      Parameters:
      path - The path to the file.
      fileAttributes - The desired FileAttributes, such as Hidden, ReadOnly,
      Normal, and Archive.

      The above has a link to the FileAttributes enumeration.

      If you look at the FileAttributes Enumeration you'll get a list of all the
      file attributes that you should be able to set.

      It includes:

      Compressed
      Encrypted
      Normal
      ReparsePoint
      SparsePoint

      I think it's a bug that you can't set these attributes with
      FileInfo.Attrib utes or File.SetAttribu tes, but can set the other attributes.

      However, I think there's a way to do these using Win 32 API calls or
      something else.

      Thanks,

      John
      ---------------------------
      "Marcin Grzębski" wrote:
      [color=blue]
      > Hi John,
      >
      > Are you sure that your file system supports these flags?
      > I think that they might be something like "reserved" or
      > "temporaly not used".
      > I know that its not an answer, but maybe you want to
      > fight with the windmills.
      >
      > Marcin
      >[color=green]
      > > How do you set the following file attributes:
      > >
      > > Compressed
      > > Encrypted
      > > Normal
      > > ReparsePoint
      > > SparsePoint
      > >
      > > You CAN'T set these using FileInfo.Attrib utes or File.SetAttribu tes. It
      > > doesn't work for these file attributes.
      > >
      > > Please show me some code how to set those attributes. If I have to use a
      > > Win 32 API call then please show me how to do it.
      > >
      > > I found you can set Archive, Hidden, NotContentIndex ed, Offline, ReadOnly,
      > > System, and Temporary using either FileInfo.Attrib utes or File.SetAttribu tes.
      > >
      > > I just need a method that works to set the remaining file attributes.
      > >
      > > Thanks,
      > >
      > > -- John...
      > >[/color]
      >[/color]

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Setting some file attributes doesn't work with FileInfo.Attrib

        You can't simply set these attributes using SetAttributes without performing
        some action on the FS object (volume, directory, file).
        For instance the reparse attribute will be set/removed as a result of a
        DeviceIOControl API call.
        DeviceIOControl is not wrapped by the FCL, so you will have to PInvoke this
        Win32 API, or simply call the fsutil.exe to perform FS operations like
        compression, reparse point setting, sparse files manipulation, etc.

        Willy.

        "John R." <johnr2000@news groups.nospam> wrote in message
        news:B78C26BF-B451-426F-9B84-4184892D5CF4@mi crosoft.com...[color=blue]
        > Take a look at this:
        >
        > File.SetAttribu tes Method
        > Sets the specified FileAttributes of the file on the specified path.
        >
        > Parameters:
        > path - The path to the file.
        > fileAttributes - The desired FileAttributes, such as Hidden, ReadOnly,
        > Normal, and Archive.
        >
        > The above has a link to the FileAttributes enumeration.
        >
        > If you look at the FileAttributes Enumeration you'll get a list of all the
        > file attributes that you should be able to set.
        >
        > It includes:
        >
        > Compressed
        > Encrypted
        > Normal
        > ReparsePoint
        > SparsePoint
        >
        > I think it's a bug that you can't set these attributes with
        > FileInfo.Attrib utes or File.SetAttribu tes, but can set the other
        > attributes.
        >
        > However, I think there's a way to do these using Win 32 API calls or
        > something else.
        >
        > Thanks,
        >
        > John
        > ---------------------------
        > "Marcin Grzebski" wrote:
        >[color=green]
        >> Hi John,
        >>
        >> Are you sure that your file system supports these flags?
        >> I think that they might be something like "reserved" or
        >> "temporaly not used".
        >> I know that its not an answer, but maybe you want to
        >> fight with the windmills.
        >>
        >> Marcin
        >>[color=darkred]
        >> > How do you set the following file attributes:
        >> >
        >> > Compressed
        >> > Encrypted
        >> > Normal
        >> > ReparsePoint
        >> > SparsePoint
        >> >
        >> > You CAN'T set these using FileInfo.Attrib utes or File.SetAttribu tes.
        >> > It
        >> > doesn't work for these file attributes.
        >> >
        >> > Please show me some code how to set those attributes. If I have to use
        >> > a
        >> > Win 32 API call then please show me how to do it.
        >> >
        >> > I found you can set Archive, Hidden, NotContentIndex ed, Offline,
        >> > ReadOnly,
        >> > System, and Temporary using either FileInfo.Attrib utes or
        >> > File.SetAttribu tes.
        >> >
        >> > I just need a method that works to set the remaining file attributes.
        >> >
        >> > Thanks,
        >> >
        >> > -- John...
        >> >[/color]
        >>[/color][/color]


        Comment

        • John R.

          #5
          Re: Setting some file attributes doesn't work with FileInfo.Attrib

          I'm not up on making API calls. You say I would use DeviceIOControl ...

          How would I do that? Do you have a sample that shows how to do that?

          Thanks,

          -- John...
          ------------------------------------
          "Willy Denoyette [MVP]" wrote:
          [color=blue]
          > You can't simply set these attributes using SetAttributes without performing
          > some action on the FS object (volume, directory, file).
          > For instance the reparse attribute will be set/removed as a result of a
          > DeviceIOControl API call.
          > DeviceIOControl is not wrapped by the FCL, so you will have to PInvoke this
          > Win32 API, or simply call the fsutil.exe to perform FS operations like
          > compression, reparse point setting, sparse files manipulation, etc.
          >
          > Willy.
          >
          > "John R." <johnr2000@news groups.nospam> wrote in message
          > news:B78C26BF-B451-426F-9B84-4184892D5CF4@mi crosoft.com...[color=green]
          > > Take a look at this:
          > >
          > > File.SetAttribu tes Method
          > > Sets the specified FileAttributes of the file on the specified path.
          > >
          > > Parameters:
          > > path - The path to the file.
          > > fileAttributes - The desired FileAttributes, such as Hidden, ReadOnly,
          > > Normal, and Archive.
          > >
          > > The above has a link to the FileAttributes enumeration.
          > >
          > > If you look at the FileAttributes Enumeration you'll get a list of all the
          > > file attributes that you should be able to set.
          > >
          > > It includes:
          > >
          > > Compressed
          > > Encrypted
          > > Normal
          > > ReparsePoint
          > > SparsePoint
          > >
          > > I think it's a bug that you can't set these attributes with
          > > FileInfo.Attrib utes or File.SetAttribu tes, but can set the other
          > > attributes.
          > >
          > > However, I think there's a way to do these using Win 32 API calls or
          > > something else.
          > >
          > > Thanks,
          > >
          > > John
          > > ---------------------------
          > > "Marcin Grzebski" wrote:
          > >[color=darkred]
          > >> Hi John,
          > >>
          > >> Are you sure that your file system supports these flags?
          > >> I think that they might be something like "reserved" or
          > >> "temporaly not used".
          > >> I know that its not an answer, but maybe you want to
          > >> fight with the windmills.
          > >>
          > >> Marcin
          > >>
          > >> > How do you set the following file attributes:
          > >> >
          > >> > Compressed
          > >> > Encrypted
          > >> > Normal
          > >> > ReparsePoint
          > >> > SparsePoint
          > >> >
          > >> > You CAN'T set these using FileInfo.Attrib utes or File.SetAttribu tes.
          > >> > It
          > >> > doesn't work for these file attributes.
          > >> >
          > >> > Please show me some code how to set those attributes. If I have to use
          > >> > a
          > >> > Win 32 API call then please show me how to do it.
          > >> >
          > >> > I found you can set Archive, Hidden, NotContentIndex ed, Offline,
          > >> > ReadOnly,
          > >> > System, and Temporary using either FileInfo.Attrib utes or
          > >> > File.SetAttribu tes.
          > >> >
          > >> > I just need a method that works to set the remaining file attributes.
          > >> >
          > >> > Thanks,
          > >> >
          > >> > -- John...
          > >> >
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • Kevin Yu [MSFT]

            #6
            Re: Setting some file attributes doesn't work with FileInfo.Attrib

            Hi John,

            First of all, I would like to confirm my understanding of your issue. From
            your description, I understand that you need to set some attributes to the
            file. However, it doens't work using the File.SetAttribu tes method. If
            there is any misunderstandin g, please feel free to let me know.

            The File.SetAttribu tes method in the .NET FX class libraries is really just
            a thin wrapper around Win32's SetFileAttribut es API.

            As far as I know, the some of the attributes cannot be set using the
            File.SetAttribu tes method. And in fact, I don't think there's any way to
            set this attribute using the .NET Framework class libraries today. I
            believe you would have to go directly to the Win32 APIs in order to
            accomplish this. (You can call Win32 APIs from C# code by using PInvoke).

            You can find the help topic for Win32's SetFileAttribut es here:

            http://msdn.microsoft.com/library/de...us/fileio/base
            /setfileattribut es.asp

            As mentioned in this help topic, the SetFileAttribut es API cannot be used
            to set any of the following file attributes:

            FILE_ATTRIBUTE_ COMPRESSED
            FILE_ATTRIBUTE_ DEVICE
            FILE_ATTRIBUTE_ DIRECTORY
            FILE_ATTRIBUTE_ ENCRYPTED
            FILE_ATTRIBUTE_ REPARSE_POINT
            FILE_ATTRIBUTE_ SPARSE_FILE

            These are special attributes that must be set through other means, like
            using the Win32 DeviceIoControl API, for example.

            The document has given us a complete list of ways to set these attributes.
            HTH.

            Kevin Yu
            =======
            "This posting is provided "AS IS" with no warranties, and confers no
            rights."

            Comment

            • John R.

              #7
              Re: Setting some file attributes doesn't work with FileInfo.Attrib

              According to you to accomplish this you can call Win32 APIs from C# code by
              using PInvoke.

              1.) Would you please show me with C# code how to use the Win32
              DeviceIoControl API or whatever other method there is to set the
              FILE_ATTRIBUTE_ COMPRESSED, FILE_ATTRIBUTE_ ENCRYPTED,
              FILE_ATTRIBUTE_ REPARSE_POINT, and FILE_ATTRIBUTE_ SPARSE_FILE attributes?

              I haven't been able to find a sample that shows how to do that and the link
              you provided doesn't show how to use it.

              2.) By the way, what is PInvoke and how does one do that?

              Thanks,

              -- John...

              -----------------------------
              "Kevin Yu [MSFT]" wrote:
              [color=blue]
              > Hi John,
              >
              > First of all, I would like to confirm my understanding of your issue. From
              > your description, I understand that you need to set some attributes to the
              > file. However, it doens't work using the File.SetAttribu tes method. If
              > there is any misunderstandin g, please feel free to let me know.
              >
              > The File.SetAttribu tes method in the .NET FX class libraries is really just
              > a thin wrapper around Win32's SetFileAttribut es API.
              >
              > As far as I know, the some of the attributes cannot be set using the
              > File.SetAttribu tes method. And in fact, I don't think there's any way to
              > set this attribute using the .NET Framework class libraries today. I
              > believe you would have to go directly to the Win32 APIs in order to
              > accomplish this. (You can call Win32 APIs from C# code by using PInvoke).
              >
              > You can find the help topic for Win32's SetFileAttribut es here:
              >
              > http://msdn.microsoft.com/library/de...us/fileio/base
              > /setfileattribut es.asp
              >
              > As mentioned in this help topic, the SetFileAttribut es API cannot be used
              > to set any of the following file attributes:
              >
              > FILE_ATTRIBUTE_ COMPRESSED
              > FILE_ATTRIBUTE_ DEVICE
              > FILE_ATTRIBUTE_ DIRECTORY
              > FILE_ATTRIBUTE_ ENCRYPTED
              > FILE_ATTRIBUTE_ REPARSE_POINT
              > FILE_ATTRIBUTE_ SPARSE_FILE
              >
              > These are special attributes that must be set through other means, like
              > using the Win32 DeviceIoControl API, for example.
              >
              > The document has given us a complete list of ways to set these attributes.
              > HTH.
              >
              > Kevin Yu
              > =======
              > "This posting is provided "AS IS" with no warranties, and confers no
              > rights."
              >
              >[/color]

              Comment

              • Kevin Yu [MSFT]

                #8
                Re: Setting some file attributes doesn't work with FileInfo.Attrib

                Hi John,

                1) Here is the C# code to call Win32 API DeviceIoControl to compress a file.

                [DllImport("kern el32.dll")]
                public static extern int DeviceIoControl (IntPtr hDevice, int
                dwIoControlCode , ref short lpInBuffer, int nInBufferSize, IntPtr
                lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned , IntPtr
                lpOverlapped);

                private static int FSCTL_SET_COMPR ESSION = 0x9C040;
                private static short COMPRESSION_FOR MAT_DEFAULT = 1;

                private void compress(string filename)
                {
                int lpBytesReturned = 0;
                FileStream f = File.Open(filen ame, System.IO.FileM ode.Open,
                System.IO.FileA ccess.ReadWrite , System.IO.FileS hare.None);
                int result = DeviceIoControl (f.Handle, FSCTL_SET_COMPR ESSION,
                ref COMPRESSION_FOR MAT_DEFAULT, 2 /*sizeof(short)*/, IntPtr.Zero, 0,
                ref lpBytesReturned , IntPtr.Zero);
                }

                2) PInvoke means platform invoke. It is using managed code to call
                unmanaged APIs. Here is a tutorial and some examples for you to learn
                PInvoke. HTH.

                http://msdn.microsoft.com/library/de...us/csref/html/
                vcwlkplatformin voketutorial.as p
                http://msdn.microsoft.com/library/de...us/cpguide/htm
                l/cpconPlatformIn vokeExamples.as p

                Kevin Yu
                =======
                "This posting is provided "AS IS" with no warranties, and confers no
                rights."

                Comment

                Working...