472,805 Members | 926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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

Nov 16 '05 #1
7 12097
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.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...

Nov 16 '05 #2
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
---------------------------
"Marcin Grzębski" wrote:
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.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...

Nov 16 '05 #3
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." <jo*******@newsgroups.nospam> wrote in message
news:B7**********************************@microsof t.com...
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
---------------------------
"Marcin Grzebski" wrote:
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.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...
>

Nov 16 '05 #4
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:
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." <jo*******@newsgroups.nospam> wrote in message
news:B7**********************************@microsof t.com...
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
---------------------------
"Marcin Grzebski" wrote:
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.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...
>


Nov 16 '05 #5
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/de...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."

Nov 16 '05 #6
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:
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/de...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."

Nov 16 '05 #7
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/de...us/csref/html/
vcwlkplatforminvoketutorial.asp
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconPlatformInvokeExamples.asp

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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: G. Richard Bellamy | last post by:
I'm trying to unset the Encrypted attribute on all the files in a path. The attribute is not getting set. What am I doing wrong? Perhaps there's another newsgroup I can send this to? Here's...
2
by: MK | last post by:
Is there any way to write into the app config at runtime? I want to store position, height and width of my forms.
3
by: Nicky | last post by:
hi, If I got a path like C:\testfile , how can I tell it is a file, not a folder? This file does not have a extension name. thanks.
8
by: Eddie Suey | last post by:
I want to add a new line to the begining of a text file. I dont want to write over existing data. How do I do this? the file is about 7 mb.
10
by: Allen | last post by:
When using FileInfo to delete a file, the file remains in the directory (not deleted) Dim foo as New FileInfo(strFileToDelete) Try foo.Delete() Catch ex as IOException 'Open file handler End...
1
by: Bruce Lawson | last post by:
Hi All, I've modified a simple Aspnet program from a text to show file information, including attributes and I keep getting an error which I cannot seem to solve; here's the important parts:...
2
by: Greg Larsen | last post by:
I'm trying write a SQLCLR that returns FileInfo.Attributes property to SQL Server in a record set and I am getting the following error. cannot convert from 'System.IO.FileAttributes' to...
2
by: rn5a | last post by:
Using the FileSystemInfo class, I am retrieving all the directories & files existing in a particular directory on the server & listing them in a ListBox. If an item in the ListBox happens to be a...
2
by: Ronald S. Cook | last post by:
Right-click on a file (e.g. a .wmv file) in Windows Explorer and there's the "General" tab with Date Created, Attributes (i.e. whether the file is read only or and.or hidden), and other properties....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.