File.Decrypt(String) 方法

定义

使用 Encrypt(String) 方法解密由当前帐户加密的文件。

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

参数

path
String

描述要解密的文件的路径。

属性

例外

.NET Framework 和 .NET Core 版本早于 2.1:参数path为零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

path 参数为 null

指定了无效的驱动器。

找不到 path 参数描述的文件。

打开文件时发生 I/O 错误。 例如,加密文件已打开。

- 或 -

当前平台不支持此操作。

指定的路径和/或文件名超过了系统定义的最大长度。

当前操作系统不是 Windows NT 或更高版本。

文件系统不是 NTFS。

path 参数指定了一个只读文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 参数指定了一个目录。

- 或 -

调用方没有所要求的权限。

示例

下面的代码示例使用 Encrypt 方法和 Decrypt 方法对文件进行加密和解密。 文件必须存在,该示例才能正常工作。

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);
        }
    }
}
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

注解

方法 Decrypt 允许解密使用 Encrypt 方法加密的文件。 方法 Decrypt 只能解密使用当前用户帐户加密的文件。

重要

此 API 仅在能够使用 NTFS 加密文件系统 (EFS) 的 Windows 平台上受支持。 任何尝试在非 Windows 系统、Windows 家庭版系统或非 NTFS 驱动器上使用此功能都会导致 PlatformNotSupportedExceptionNotSupportedException,具体取决于具体情况。

不建议在 .NET Core 中使用此 API;它是为了为迁移到 .NET Core 但仍显式面向 Windows 的应用程序启用可移植性。

方法 Decrypt 需要对正在解密的文件具有独占访问权限,如果另一个进程正在使用该文件,将引发异常。 如果文件未加密, Decrypt 将返回一个非零值,指示成功。

Encrypt方法和 Decrypt 方法都使用安装在计算机上的加密服务提供程序 (CSP) ,以及调用方法的进程的文件加密密钥。

当前文件系统的格式必须为 NTFS,并且当前操作系统必须Windows NT或更高版本。

适用于