FileInfo.Encrypt 메서드

정의

파일을 암호화하는 데 사용된 계정으로만 해독할 수 있도록 암호화합니다.

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

예외

잘못된 드라이브를 지정했습니다.

현재 FileInfo 개체에 설명된 파일을 찾을 수 없습니다.

파일을 여는 동안 I/O 오류가 발생했습니다.

NTFS 파일 시스템이 아닙니다.

현재 운영 체제가 Microsoft Windows NT 이상이 아닙니다.

현재 FileInfo 개체가 설명하는 파일이 읽기 전용입니다.

또는 현재 플랫폼이 해당 작업을 지원하지 않는 경우

또는 호출자에게 필요한 권한이 없는 경우

예제

다음 코드 예제에서는 메서드와 Decrypt 메서드를 사용하여 Encrypt 파일을 암호화한 다음 암호를 해독합니다.

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 메서드를 사용하면 이 메서드를 호출하는 데 사용되는 계정만 암호를 해독할 수 있도록 파일을 암호화할 수 있습니다. 메서드를 Decrypt 사용하여 메서드로 암호화된 파일의 암호를 해독합니다 Encrypt .

Encrypt 메서드와 Decrypt 메서드는 모두 컴퓨터에 설치된 CSP(암호화 서비스 공급자)와 메서드를 호출하는 프로세스의 파일 암호화 키를 사용합니다.

현재 파일 시스템의 형식은 NTFS여야 하며 현재 운영 체제는 Microsoft Windows NT 이상이어야 합니다.

적용 대상