FileInfo.Decrypt Methode

Definition

Entschlüsselt eine Datei, die vom aktuellen Konto mit der Encrypt()-Methode verschlüsselt wurde.

public:
 void Decrypt();
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public void Decrypt ();
public void Decrypt ();
[System.Runtime.InteropServices.ComVisible(false)]
public void Decrypt ();
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.Decrypt : unit -> unit
member this.Decrypt : unit -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Decrypt : unit -> unit
Public Sub Decrypt ()
Attribute

Ausnahmen

Es wurde ein ungültiges Laufwerk angegeben.

Die durch das aktuelle FileInfo-Objekt beschriebene Datei konnte nicht gefunden werden.

Beim Öffnen der Datei ist ein E/A-Fehler aufgetreten.

Das Dateisystem ist nicht NTFS.

Das aktuelle Betriebssystem ist nicht Microsoft Windows NT oder höher.

Die durch das aktuelle FileInfo-Objekt beschriebene Datei ist schreibgeschützt.

- oder -

Dieser Vorgang wird von der aktuellen Plattform nicht unterstützt.

- oder -

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Beispiele

Im folgenden Codebeispiel werden die Encrypt -Methode und die Decrypt -Methode verwendet, um eine Datei zu verschlüsseln und dann zu entschlüsseln.

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

Hinweise

Mit Decrypt der -Methode können Sie eine Datei entschlüsseln, die mit der Encrypt -Methode verschlüsselt wurde. Die Decrypt -Methode kann nur Dateien entschlüsseln, die mit dem aktuellen Benutzerkonto verschlüsselt wurden.

Sowohl die Encrypt -Methode als auch die Decrypt -Methode verwenden den auf dem Computer installierten Kryptografiedienstanbieter (Cryptographic Service Provider, CSP) und die Dateiverschlüsselungsschlüssel des Prozesses, der die Methode aufruft.

Das aktuelle Dateisystem muss als NTFS formatiert sein, und das aktuelle Betriebssystem muss Microsoft Windows NT oder höher sein.

Gilt für: