FileInfo.Decrypt 方法

定义

使用 Encrypt() 方法解密由当前帐户加密的文件。Decrypts a file that was encrypted by the current account using the Encrypt() method.

public:
 void Decrypt();
[System.Runtime.InteropServices.ComVisible(false)]
public void Decrypt ();
member this.Decrypt : unit -> unit
Public Sub Decrypt ()
属性

异常

指定了无效的驱动器。An invalid drive was specified.

找不到当前 FileInfo 对象描述的文件。The file described by the current FileInfo object could not be found.

打开文件时发生 I/O 错误。An I/O error occurred while opening the file.

文件系统不是 NTFS。The file system is not NTFS.

当前操作系统不是 Microsoft Windows NT 或更高版本。The current operating system is not Microsoft Windows NT or later.

当前 FileInfo 对象描述的文件为只读文件。The file described by the current FileInfo object is read-only.

- 或 --or- 当前平台不支持此操作。This operation is not supported on the current platform.

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

using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;


    static void Addencryption(String^ fileName)
{
    // Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }
    // Add encryption.
    fInfo->Encrypt();
    
}


    static void Removeencryption(String^ fileName)
{
//    Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }
    // Remove encryption.
    fInfo->Decrypt();
}

int main()
{
    try
    {
        String^ fileName = "c:\\MyTest.txt";
        Console::WriteLine("Encrypt " + fileName);

        // Encrypt the file.
     
        Addencryption(fileName);
        Console::WriteLine("Decrypt " + fileName);

        // Decrypt the file.
        Removeencryption(fileName);
        Console::WriteLine("Done");
     }
     catch (IOException^ ex)
     {
        Console::WriteLine(ex->Message);
     }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//Encrypt c:\MyTest.txt
//Decrypt c:\MyTest.txt
//Done
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = @"c:\MyTest.txt";

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

        public static void AddEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);
            if (!fInfo.Exists)
            {
                //Create the file.
                fInfo.Create();
            }
            // Add encryption.
            fInfo.Encrypt();
        }

        public static void RemoveEncryption(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);
            if (!fInfo.Exists)
            {
                //Create the file.
                fInfo.Create();
            }
            // Remove encryption.
            fInfo.Decrypt();

        }
    }
}

//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//Encrypt c:\MyTest.txt
//Decrypt c:\MyTest.txt
//Done
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "c:\MyTest.txt"

            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

    End Sub



    Sub AddEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Add encryption.
        fInfo.Encrypt()

    End Sub



    Sub RemoveEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Remove encryption.
        fInfo.Decrypt()

    End Sub
End Module
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'Encrypt c:\MyTest.txt
'Decrypt c:\MyTest.txt
'Done

注解

使用方法, 可以对使用方法加密的文件进行解密。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.

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 格式, 并且当前操作系统必须是 Microsoft Windows NT 或更高版本。The current file system must be formatted as NTFS and the current operating system must be Microsoft Windows NT or later.

安全性

FileIOPermission
关联的枚举Read:WriteAssociated enumerations: Read, Write 安全操作:手动.Security action: Demand. 用于读取和写入当前FileInfo对象所描述的文件的权限。For permission to read and write to the file described by the current FileInfo object.

适用于