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 namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;


    static void Addencryption(String^ fileName)
{
    // Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }
    // Add encryption.
    fInfo->Encrypt();
    
}


    static void Removeencryption(String^ fileName)
{
//    Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }
    // Remove encryption.
    fInfo->Decrypt();
}

int 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 (IOException^ ex)
     {
        Console::WriteLine(ex->Message);
     }
}
//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
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 檔案。

方法與 Decrypt 方法都會 Encrypt 使用安裝在電腦上的密碼編譯服務提供者 (CSP) ,以及呼叫 方法之進程的檔案加密金鑰。

目前的檔案系統必須格式化為 NTFS,而且目前的作業系統必須Microsoft Windows NT或更新版本。

適用於