FileInfo.Encrypt 方法

定义

加密文件,以便只有用于加密文件的帐户才能解密该文件。

public:
 void Encrypt();
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public void Encrypt();
public void Encrypt();
[System.Runtime.InteropServices.ComVisible(false)]
public void Encrypt();
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.Encrypt : unit -> unit
member this.Encrypt : unit -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Encrypt : unit -> unit
Public Sub Encrypt ()
属性

例外

指定了无效驱动器。

找不到当前 FileInfo 对象描述的文件。

打开文件时出现 I/O 错误。

文件系统不是 NTFS。

当前操作系统未Microsoft Windows NT 或更高版本。

当前 FileInfo 对象描述的文件是只读的。

-或-

当前平台上不支持此操作。

-或-

调用方没有所需的权限。

示例

下面的代码示例使用 Encrypt 该方法和 Decrypt 方法加密文件,然后对其进行解密。

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = @"c:\MyTest.txt";

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

        public static void AddEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);
            if (!fInfo.Exists)
            {
                //Create the file.
                fInfo.Create();
            }
            // Add encryption.
            fInfo.Encrypt();
        }

        public static void RemoveEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);
            if (!fInfo.Exists)
            {
                //Create the file.
                fInfo.Create();
            }
            // Remove encryption.
            fInfo.Decrypt();
        }
    }
}

//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Encrypt c:\MyTest.txt
//Decrypt c:\MyTest.txt
//Done
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "c:\MyTest.txt"

            Console.WriteLine("Encrypt " + FileName)

            ' Encrypt the file.
            AddEncryption(FileName)

            Console.WriteLine("Decrypt " + FileName)

            ' Decrypt the file.
            RemoveEncryption(FileName)

            Console.WriteLine("Done")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

    End Sub



    Sub AddEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Add encryption.
        fInfo.Encrypt()

    End Sub



    Sub RemoveEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Remove encryption.
        fInfo.Decrypt()

    End Sub
End Module
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'Encrypt c:\MyTest.txt
'Decrypt c:\MyTest.txt
'Done

注解

该方法 Encrypt 允许加密文件,以便只有用于调用此方法的帐户才能解密它。 使用该方法 Decrypt 解密由 Encrypt 该方法加密的文件。

Encrypt该方法和Decrypt方法都使用计算机上安装的加密服务提供程序(CSP),以及调用该方法的进程的文件加密密钥。

当前文件系统的格式必须为 NTFS,并且当前操作系统必须Microsoft Windows NT 或更高版本。

适用于