File.Encrypt(String) 메서드

정의

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

public:
 static void Encrypt(System::String ^ path);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void Encrypt (string path);
public static void Encrypt (string path);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Encrypt : string -> unit
static member Encrypt : string -> unit
Public Shared Sub Encrypt (path As String)

매개 변수

path
String

암호화할 파일을 나타내는 경로입니다.

특성

예외

.NET Framework 및 .NET Core 버전 2.1 이전: path 매개 변수는 길이가 0인 문자열이거나 공백만 포함하거나 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path 매개 변수가 null인 경우

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

path 매개 변수에 설명된 파일을 찾을 수 없습니다.

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

또는

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

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

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

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

path 매개 변수가 읽기 전용 파일을 지정합니다.

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

또는 path 매개 변수가 디렉터리를 지정합니다.

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

예제

다음 코드 예제에서는 메서드와 메서드를 Decrypt 사용하여 Encrypt 파일을 암호화한 다음 암호를 해독합니다. 예제가 작동하려면 파일이 있어야 합니다.

using namespace System;
using namespace System::IO;

int main()
{
    String^ fileName = "test.xml";
    if (!File::Exists(fileName))
    {
        Console::WriteLine("The file " + fileName
            + " does not exist.");
        return 0;
    }
    try
    {
        Console::WriteLine("Encrypt " + fileName);

        // Encrypt the file.
        File::Encrypt(fileName);

        Console::WriteLine("Decrypt " + fileName);

        // Decrypt the file.
        File::Decrypt(fileName);

        Console::WriteLine("Done");
    }
    catch (IOException^ ex)
    {
        Console::WriteLine("There was an IO problem.");
        Console::WriteLine(ex->Message);
    }
    catch (PlatformNotSupportedException^)
    {
        Console::WriteLine("Encryption is not supported on " +
            "this system.");
    }
    catch (NotSupportedException^)
    {
        Console::WriteLine("Encryption is not supported on " +
            "this system.");
    }
    catch (UnauthorizedAccessException^)
    {
        Console::WriteLine("The operation could not be "
            + "carried out.");
    }
}
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "test.xml";

                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);
            }

            Console.ReadLine();
        }

        // Encrypt a file.
        public static void AddEncryption(string FileName)
        {

            File.Encrypt(FileName);
        }

        // Decrypt a file.
        public static void RemoveEncryption(string FileName)
        {
            File.Decrypt(FileName);
        }
    }
}
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "test.xml"

            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

        Console.ReadLine()

    End Sub


    ' Encrypt a file.
    Sub AddEncryption(ByVal FileName As String)

        File.Encrypt(FileName)

    End Sub


    ' Decrypt the file.
    Sub RemoveEncryption(ByVal FileName As String)

        File.Decrypt(FileName)

    End Sub
End Module

설명

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

중요

이 API는 NTFS EFS(암호화 파일 시스템)를 사용할 수 있는 Windows 플랫폼에서만 지원됩니다. 비 Windows 시스템, Windows Home Edition 시스템 또는 비 NTFS 드라이브에서 PlatformNotSupportedException NotSupportedException이를 사용하려는 시도는 상황에 따라 또는 그 결과로 발생합니다.

.NET Core에서 이 API를 사용하지 않는 것이 좋습니다. .NET Core로 이동하지만 여전히 명시적으로 Windows를 대상으로 하는 애플리케이션에 이식성을 사용하도록 설정하기 위해 포함됩니다.

이 메서드는 Encrypt 암호화되는 파일에 대한 단독 액세스가 필요하며 다른 프로세스가 파일을 사용하는 경우 실패합니다.

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

이 메서드는 모든 버전의 Windows에서 사용할 수 없습니다. 예를 들어 홈 버전에서는 사용할 수 없습니다.

현재 파일 시스템의 형식은 NTFS여야 합니다.

적용 대상