FileInfo.Decrypt Méthode

Définition

Déchiffre un fichier qui a été chiffré par le compte actuel à l'aide de la méthode Encrypt().

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

Exceptions

Un lecteur non valide a été spécifié.

Le fichier décrit par l’objet FileInfo actuel est introuvable.

Une erreur d’E/S s’est produite lors de l’ouverture du fichier.

Le système de fichiers n’est pas NTFS.

Le système d’exploitation actuel n’est pas Microsoft Windows NT ou une version ultérieure.

Le fichier décrit par l’objet FileInfo actuel est accessible en lecture seule.

- ou -

Cette opération n'est pas prise en charge sur la plateforme actuelle.

- ou -

L'appelant n'a pas l'autorisation requise.

Exemples

L’exemple de code suivant utilise la Encrypt méthode et la Decrypt méthode pour chiffrer, puis déchiffrer un fichier.

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

Remarques

La Decrypt méthode vous permet de déchiffrer un fichier qui a été chiffré à l’aide de la Encrypt méthode . La Decrypt méthode peut déchiffrer uniquement les fichiers qui ont été chiffrés à l’aide du compte d’utilisateur actuel.

Encrypt La méthode et la Decrypt méthode utilisent le fournisseur de services de chiffrement (CSP) installé sur l’ordinateur et les clés de chiffrement de fichier du processus appelant la méthode.

Le système de fichiers actuel doit être au format NTFS et le système d’exploitation actuel doit être Microsoft Windows NT ou version ultérieure.

S’applique à