473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 12196
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.