語言

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 物件所描述的檔案是唯讀的。

-或-

目前平臺不支援此作業。

-或-

來電者沒有所需的權限。

範例

以下程式碼範例使用了 方法 Encrypt ,以及加密 Decrypt 檔案後解密的方法。

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,且目前作業系統必須是 NT 或更新Microsoft Windows。

適用於