語言

File.Encrypt(String) 方法

定義

加密檔案,讓只有用來加密檔案的帳戶才能解密它。

public:
 static void Encrypt(System::String ^ path);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void Encrypt(string path);
public static void Encrypt(string path);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Encrypt : string -> unit
static member Encrypt : string -> unit
Public Shared Sub Encrypt (path As String)

參數

path
String

一條描述要加密檔案的路徑。

屬性

例外狀況

.NET Framework 及 2.1 .NET Core 版本:path 參數為零長度字串,僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

參數 path 為 null。

指定了一個無效的磁碟機。

找不到該 path 參數所描述的檔案。

開啟檔案時發生 I/O 錯誤。

-或-

目前平臺不支援此作業。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

目前的操作系統不是 Windows NT 或更新版本。

這個檔案系統不是 NTFS。

該 path 參數指定一個唯讀的檔案。

-或-

目前平臺不支援此作業。

-或-

該 path 參數指定了一個目錄。

-或-

來電者沒有所需的權限。

範例

以下程式碼範例使用了EncryptDecrypt加密與解密檔案的方法。 該檔案必須存在,範例才會運作。

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);
        }
    }
}
open System.IO

// Encrypt a file.
let addEncryption fileName = File.Encrypt fileName

// Decrypt a file.
let removeEncryption fileName = File.Decrypt fileName

let fileName = "test.xml"

printfn $"Encrypt {fileName}"

// Encrypt the file.
addEncryption fileName

printfn $"Decrypt {fileName}"

// Decrypt the file.
removeEncryption fileName

printfn "Done"
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "test.xml"

            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

        Console.ReadLine()

    End Sub


    ' Encrypt a file.
    Sub AddEncryption(ByVal FileName As String)

        File.Encrypt(FileName)

    End Sub


    ' Decrypt the file.
    Sub RemoveEncryption(ByVal FileName As String)

        File.Decrypt(FileName)

    End Sub
End Module

備註

此 Encrypt 方法允許你加密檔案,只有呼叫此方法的帳號能解密。 使用該 Decrypt 方法來解密被該 Encrypt 方法加密的檔案。

Important

此 API 僅支援能使用 NTFS 加密檔案系統(EFS)的 Windows 平台。 任何嘗試在非Windows系統、Windows家用版或非NTFS硬碟上使用此方法,根據情況,會出現PlatformNotSupportedException或NotSupportedException。

不建議在 .NET Core 中使用此 API;此功能是為了讓遷移至 .NET Core 但仍明確針對 Windows 的應用程式提供可攜性。

此 Encrypt 方法需要對被加密檔案的專屬存取權,若有其他程序使用該檔案,則會失敗。

此 Encrypt 方法與方法 Decrypt 皆使用安裝在電腦上的密碼服務提供者(CSP)及呼叫該方法程序的檔案加密金鑰。

此方法並非所有版本的 Windows 都能使用。 例如,它在家庭版中沒有。

目前的檔案系統必須格式為 NTFS。

適用於