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

G

Guest

How do you set the following file attributes:

Compressed
Encrypted
Normal
ReparsePoint
SparsePoint

You CAN'T set these using FileInfo.Attributes or File.SetAttributes. 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, NotContentIndexed, Offline, ReadOnly,
System, and Temporary using either FileInfo.Attributes or File.SetAttributes.

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

Thanks,

-- John...
 
G

Guest

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
 
G

Guest

Take a look at this:

File.SetAttributes 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.Attributes or File.SetAttributes, 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
---------------------------
 
W

Willy Denoyette [MVP]

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.
 
G

Guest

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...
 
K

Kevin Yu [MSFT]

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.SetAttributes method. If
there is any misunderstanding, please feel free to let me know.

The File.SetAttributes method in the .NET FX class libraries is really just
a thin wrapper around Win32's SetFileAttributes API.

As far as I know, the some of the attributes cannot be set using the
File.SetAttributes 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 SetFileAttributes here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base
/setfileattributes.asp

As mentioned in this help topic, the SetFileAttributes 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."
 
G

Guest

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...
 
K

Kevin Yu [MSFT]

Hi John,

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

[DllImport("kernel32.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_COMPRESSION = 0x9C040;
private static short COMPRESSION_FORMAT_DEFAULT = 1;

private void compress(string filename)
{
int lpBytesReturned = 0;
FileStream f = File.Open(filename, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
int result = DeviceIoControl(f.Handle, FSCTL_SET_COMPRESSION,
ref COMPRESSION_FORMAT_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/default.asp?url=/library/en-us/csref/html/
vcwlkplatforminvoketutorial.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconPlatformInvokeExamples.asp

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top