Export (0) Print
Expand All

FileInfo.Encrypt Method

Note: This method is new in the .NET Framework version 2.0.

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

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

[ComVisibleAttribute(false)] 
public void Encrypt ()
/** @attribute ComVisibleAttribute(false) */ 
public void Encrypt ()
ComVisibleAttribute(false) 
public function Encrypt ()

Exception typeCondition

DriveNotFoundException

An invalid drive was specified.

FileNotFoundException

The file described by the current FileInfo object could not be found.

IOException

An I/O error occurred while opening the file.

NotSupportedException

The file system is not NTFS.

PlatformNotSupportedException

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

UnauthorizedAccessException

The file described by the current FileInfo object is read-only.

-or-

This operation is not supported on the current platform.

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

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 a file and then 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("Enceypt " + 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();
        }

        
        public static void AddEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Add encryption.
            fInfo.Encrypt();

        }

        
        public static void RemoveEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Remove encryption.
            fInfo.Decrypt();

        }
    }
}

  • FileIOPermission  Associated enumerations: Read, Write Security action: Demand. For permission to read and write to the file described by the current FileInfo object.

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft