Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How FileAttributes.Encrypted work in C#?

I didn't find any example on google... Can anyone show how it works? And when folders and files will be encrypted how to decrypt them?

Link: FileAttributes Enumeration

like image 449
a1204773 Avatar asked Oct 13 '12 17:10

a1204773


2 Answers

The FileAttributes.Encrypted flag tells you whether a file or folder has been encrypted by the NTFS file system. This option is available to users by right-clicking a file, selecting Properties, then clicking the Advanced button. There is an option "Encrypt contents to secure data" which is what the FileAttributes.Encrypted flag corresponds to.

You can use the File.Encrypt / File.Decrypt methods to encrypt or decrypt a file or folder. Note that you can only decrypt something that was encrypted by the current user.

You can use the System.IO.File.GetAttributes method to get the FileAttributes from a file.

like image 153
Mike Zboray Avatar answered Oct 16 '22 20:10

Mike Zboray


Simply use the following piece of code for File encryption identification:

FileAttributes attributes = File.GetAttributes("C:\testfile.txt");
if ((attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted)
{
    Console.WriteLine("Encrypted file");
}
else
{
    Console.WriteLine("Not Encrypted file");
}

Happy Coding...

like image 3
Furqan Safdar Avatar answered Oct 16 '22 19:10

Furqan Safdar