.Net 4 One Line File.Encrypt

http://msdn.microsoft.com/en-us/library/system.io.file.encrypt.aspx

Encrypts a file so that only the account used to encrypt the file can decrypt it.

using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "test.xml";
                Console.WriteLine("Encrypt " + FileName);
                // Encrypt the file.
                AddEncryption(FileName);
                Console.WriteLine("Decrypt " + FileName);
                // Decrypt the file.
                RemoveEncryption(FileName);
                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
        }
        // Encrypt a file.
        public static void AddEncryption(string FileName)
        {
            File.Encrypt(FileName);
        }
        // Decrypt a file.
        public static void RemoveEncryption(string FileName)
        {
            File.Decrypt(FileName);
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s