File.Decrypt(String) File.Decrypt(String) File.Decrypt(String) File.Decrypt(String) Method

定義

解密目前帳戶使用 Encrypt(String) 方法加密的檔案。Decrypts a file that was encrypted by the current account using the Encrypt(String) method.

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

參數

path
String String String String

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

例外狀況

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. 例如,加密的檔案已經開啟。For example, the encrypted file is already open.

-或--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使用方法加密的檔案解密。 DecryptThe Decrypt method allows you to decrypt a file that was encrypted using the Encrypt method. Decrypt方法只能解密使用目前使用者帳戶加密的檔案。The Decrypt method can decrypt only files that were encrypted using the current user account.

重要

只有能夠使用 NTFS 加密檔案系統 (EFS) 的 Windows 平臺才支援這個 API。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.

不建議在 .NET Core 中使用此 API;其中包含為移至 .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.

Decrypt方法需要對要解密之檔案的獨佔存取權, 而且如果有其他進程正在使用該檔案, 就會引發例外狀況。The Decrypt method requires exclusive access to the file being decrypted, and will raise an exception if another process is using the file. 如果檔案未加密, Decrypt則會傳回非零值, 表示成功。If the file is not encrypted, Decrypt will return a nonzero value, which indicates success.

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.

目前的檔案系統必須格式化為 NTFS, 而目前的作業系統必須是 Windows NT 或更新版本。The current file system must be formatted as NTFS and the current operating system must be Windows NT or later.

安全性

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

適用於