Export (0) Print
Expand All

File.Encrypt Method

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static void Encrypt (
	string path
)
public static void Encrypt (
	String path
)
public static function Encrypt (
	path : String
)
Not applicable.

Parameters

path

A path that describes a file to encrypt.

Exception typeCondition

ArgumentException

The path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException

The path parameter is a null reference (Nothing in Visual Basic).

DriveNotFoundException

An invalid drive was specified.

FileNotFoundException

The file described by the path parameter could not be found.

IOException

An I/O error occurred while opening the file.

-or-

This operation is not supported on the current platform.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

PlatformNotSupportedException

The current operating system is not Microsoft Windows NT or later.

NotSupportedException

The file system is not NTFS.

UnauthorizedAccessException

The path parameter specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

The path parameter specified a directory.

-or-

The caller does not have the required permission.

The Encrypt method allows you to encrypt a file so that only the account used to call this method can decrypt it. Use the Decrypt method to decrypt a file encrypted by the Encrypt method.

The Encrypt method requires exclusive access to the file being encrypted, and will fail if another process is using the file.

Both the Encrypt method and the Decrypt method use the cryptographic service provider (CSP) installed on the computer and the file encryption keys of the process calling the method.

The current file system must be formatted as NTFS and the current operating system must be Microsoft Windows NT or later.

The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file.

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

  • FileIOPermission  for permission to read and write to the file described by the path parameter. Associated enumerations: Read, Write Security action: Demand.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft