FileInfo.Decrypt Metod

Definition

Dekrypterar en fil som krypterades av det aktuella kontot med hjälp av Encrypt() metoden .

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

Undantag

En ogiltig enhet har angetts.

Det gick inte att hitta filen som beskrivs av det aktuella FileInfo objektet.

Ett I/O-fel uppstod när filen öppnades.

Filsystemet är inte NTFS.

Det aktuella operativsystemet är inte Microsoft Windows NT eller senare.

Filen som beskrivs av det aktuella FileInfo objektet är skrivskyddad.

-eller-

Den här åtgärden stöds inte på den aktuella plattformen.

-eller-

Anroparen har inte den behörighet som krävs.

Exempel

I följande kodexempel används Encrypt metoden och Decrypt metoden för att kryptera och sedan dekryptera en fil.

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

Kommentarer

Med Decrypt metoden kan du dekryptera en fil som krypterades med hjälp av Encrypt metoden. Metoden Decrypt kan endast dekryptera filer som har krypterats med det aktuella användarkontot.

Encrypt Både metoden och Decrypt metoden använder den kryptografiska tjänstprovidern (CSP) som är installerad på datorn och filkrypteringsnycklarna för den process som anropar metoden.

Det aktuella filsystemet måste formateras som NTFS och det aktuella operativsystemet måste vara Microsoft Windows NT eller senare.

Gäller för