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

備註

Decrypt方法可讓您將使用加密檔案解密Encrypt方法。The 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.

重要

這個 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.

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. 相關聯的列舉型別: ReadAssociated enumerations: Read, Write

適用於