FileInfo.Decrypt Metodo

Definizione

Decrittografa un file crittografato dall'account corrente usando il metodo Encrypt().Decrypts a file that was encrypted by the current account using the Encrypt() method.

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 ()
Attributi

Eccezioni

È stata specificata un'unità non valida.An invalid drive was specified.

Il file descritto dall'oggetto FileInfo corrente non è stato trovato.The file described by the current FileInfo object could not be found.

Si è verificato un errore di I/O durante l'apertura del file.An I/O error occurred while opening the file.

Il file system non è NTFS.The file system is not NTFS.

Il sistema operativo corrente non è Microsoft Windows NT o versioni successive.The current operating system is not Microsoft Windows NT or later.

Il file descritto dall'oggetto FileInfo corrente è di sola lettura.The file described by the current FileInfo object is read-only.

-oppure--or- L'operazione non è supportata sulla piattaforma corrente.This operation is not supported on the current platform.

-oppure--or- Il chiamante non dispone dell'autorizzazione richiesta.The caller does not have the required permission.

Esempio

Nell'esempio di codice seguente vengono usati il Encrypt metodo e il Decrypt metodo per crittografare e quindi decrittografare un file.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

Commenti

Il Decrypt metodo consente di decrittografare un file crittografato utilizzando il Encrypt metodo.The Decrypt method allows you to decrypt a file that was encrypted using the Encrypt method. Il Decrypt metodo può decrittografare solo i file che sono stati crittografati con l'account utente corrente.The Decrypt method can decrypt only files that were encrypted using the current user account.

Sia il Encrypt metodo che il Decrypt metodo utilizzano il provider del servizio di crittografia (CSP) installato nel computer e le chiavi di crittografia del file del processo che chiama il metodo.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.

Il file system corrente deve essere formattato come NTFS e il sistema operativo corrente deve essere Microsoft Windows NT o versione successiva.The current file system must be formatted as NTFS and the current operating system must be Microsoft Windows NT or later.

Si applica a