473,385 Members | 1,875 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,385 software developers and data experts.

Setting EFS using FileInfo.Attributes

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 the code:
=============================================
using System;

using System.Diagnostics;

using System.IO;

namespace efsfind

{

/// <summary>

/// Searches a path, looking for EFS attribute.

/// Does not handle trailing spaces in names.

/// </summary>

class ConsoleMain

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

ConsoleMain cm = new ConsoleMain();

Arguments commandLine = new Arguments(args);

string path = commandLine["path"];

if (null != path) {

cm.ProcessDirectory (path);

}

}

private void ProcessDirectory(string path)

{

//Console.WriteLine ("{0}", path);

if (null != path && 0 < path.Length)

{

try

{

DirectoryInfo dir = new DirectoryInfo(path);

foreach (FileSystemInfo fileSystemInfo in
dir.GetFileSystemInfos ())

{

if (fileSystemInfo is FileInfo)

{

FileInfo file = (FileInfo) fileSystemInfo;

FileAttributes fileAttrib = file.Attributes;

if (FileAttributes.Encrypted ==
(FileAttributes.Encrypted & fileAttrib))

{

Console.Write ("{0}::[{1}", file.FullName,
fileAttrib);

fileAttrib = fileAttrib &
(~FileAttributes.Encrypted);

file.Attributes = fileAttrib;

file.Refresh ();

Console.WriteLine (" ==> {0}]", fileAttrib);

Console.WriteLine ("After update: {0}",
file.Attributes);

}

}

else

{

ProcessDirectory (fileSystemInfo.FullName);

}

}

}

catch (Exception e)

{

Console.WriteLine (e);

}

}

}

}

}

=============================================
Jul 21 '05 #1
4 5947
Sorry, there's a library I use that I didn't include. Therefore, these
lines:

Arguments commandLine = new Arguments(args);

string path = commandLine["path"];

Should be
string path = args[0];
Jul 21 '05 #2
Hi Richard,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to set encryption attributes
to the file. However, it doens't work by simply reverse the Attribute
property. If there is any misunderstanding, please feel free to let me know.

Setting attribute in using .NET framework 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 by simply
setting the property. 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. The
document has given us a complete list of ways to set these attributes. For
example, to unset the Encrypted attribute on the file, you have to call
Decrypt API funtion.

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

HTH.

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

Jul 21 '05 #3
Excellent, that puts me on the right track.
Jul 21 '05 #4
You're welcome, Richard.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

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

Jul 21 '05 #5

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

Similar topics

1
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the example is here :...
7
by: John R. | last post by:
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...
2
by: Samuel Shum | last post by:
Hello, I'm just wondering if there are any .Net objects that I can use in the framework that set folders/files permission, assign quotas to users. Any suggestions/comments are welcome. Thanks in...
5
by: Lance | last post by:
I want to expose properties of a class to a user via a PropertyGrid class. Some of the properties are of type System.IO.FileInfo. Ideally, an OpenFileDialog window would appear when the user...
1
by: Benjamin | last post by:
I am trying to make a file I created a hidden file. I think I need to use the setfileattribute() but I cannot find it anywhere. I found this on msdn:...
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: 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...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.