File.Encrypt(String) File.Encrypt(String) File.Encrypt(String) File.Encrypt(String) Method

定義

加密檔案,只有使用加密檔案的帳戶才能將其解密。Encrypts a file so that only the account used to encrypt the file can decrypt it.

public:
 static void Encrypt(System::String ^ path);
public static void Encrypt (string path);
static member Encrypt : string -> unit
Public Shared Sub Encrypt (path As String)

參數

path
String String String String

路徑,描述要加密的檔案。A path that describes a file to encrypt.

例外狀況

path 參數為零長度字串,只包含空格或包含一或多個由 InvalidPathChars 所定義的無效字元。The path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的磁碟機無效。An invalid drive was specified.

找不到 path 參數所描述的檔案。The file described by the path parameter could not be found.

開啟檔案時發生 I/O 錯誤。An I/O error occurred while opening the file.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

目前的作業系統不是 Windows NT 或更新版本。The current operating system is not Windows NT or later.

檔案系統不是 NTFS。The file system is not NTFS.

path 參數指定了唯讀的檔案。The path parameter specified a file that is read-only.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

-或--or- path 參數指定了目錄。The path parameter specified a directory.

-或--or- 呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列程式碼範例會使用Encrypt方法和Decrypt方法來加密和解密檔案。The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file. 檔案必須存在,範例才能運作。The file must exist for the example to work.

using namespace System;
using namespace System::IO;

int main()
{
    String^ fileName = "test.xml";
    if (!File::Exists(fileName))
    {
        Console::WriteLine("The file " + fileName
            + " does not exist.");
        return 0;
    }
    try
    {
        Console::WriteLine("Encrypt " + fileName);

        // Encrypt the file.
        File::Encrypt(fileName);

        Console::WriteLine("Decrypt " + fileName);

        // Decrypt the file.
        File::Decrypt(fileName);

        Console::WriteLine("Done");
    }
    catch (IOException^ ex)
    {
        Console::WriteLine("There was an IO problem.");
        Console::WriteLine(ex->Message);
    }
    catch (PlatformNotSupportedException^)
    {
        Console::WriteLine("Encryption is not supported on " +
            "this system.");
    }
    catch (NotSupportedException^)
    {
        Console::WriteLine("Encryption is not supported on " +
            "this system.");
    }
    catch (UnauthorizedAccessException^)
    {
        Console::WriteLine("The operation could not be "
            + "carried out.");
    }
}
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);
        }
    }
}
Imports System
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方法可讓您加密檔案,以便只將用來呼叫這個方法的帳戶可以將它解密。The Encrypt method allows you to encrypt a file so that only the account used to call this method can decrypt it. 使用Decrypt方法來解密加密的檔案Encrypt方法。Use the Decrypt method to decrypt a file encrypted by the Encrypt method.

重要

這個 API 只有在能夠使用 NTFS 加密檔案系統 (EFS) 的 Windows 平台上執行。This API is only supported on Windows platforms that are able to use the NTFS Encrypting File System (EFS). 嘗試在非 Windows 系統、 Windows Home Edition 系統或非 NTFS 磁碟機結果中的使用這PlatformNotSupportedExceptionNotSupportedException,取決於這種情況。Any attempt to use this on non-Windows systems, Windows Home Edition systems, or non-NTFS drives results in a PlatformNotSupportedException or NotSupportedException, depending on the situation.

不建議使用此 API 在.NET Core 中;它會包含啟用的應用程式,移至.NET Core,但仍會明確地以 Windows 為目標的可攜性。Use of this API in .NET Core is not recommended; it is included to enable portability for applications that move to .NET Core but still explicitly target Windows.

Encrypt方法需要獨佔存取權所加密的檔案,而且如果另一個處理序正在使用檔案將會失敗。The Encrypt method requires exclusive access to the file being encrypted, and will fail if another process is using the file.

這兩個Encrypt方法和Decrypt方法使用電腦上安裝並呼叫方法的程序的檔案加密金鑰的密碼編譯服務提供者 (CSP)。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.

這個方法並不適用於所有版本的 Windows。This method is not available on all versions of Windows. 比方說,它並不適用於家用版。For example, it is not available on Home editions.

目前的檔案系統必須格式化為 NTFS。The current file system must be formatted as NTFS.

安全性

FileIOPermission
用於讀取和寫入所描述之檔案的使用權限path參數。for permission to read and write to the file described by the path parameter. 安全性動作:需求。Security action: Demand. 相關聯的列舉型別: ReadWriteAssociated enumerations: Read, Write

適用於