Setting EFS using FileInfo.Attributes

  • Thread starter G. Richard Bellamy
  • Start date
G

G. Richard Bellamy

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);

}

}

}

}

}

=============================================
 
G

G. Richard Bellamy

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];
 
K

Kevin Yu [MSFT]

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/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. 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/default.asp?url=/library/en-us/fileio/base
/decryptfile.asp

HTH.

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

Kevin Yu [MSFT]

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

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