1
Decrypt 方法允许解密使用 Encrypt 方法加密的文件。Decrypt 方法只能解密使用当前用户帐户加密的文件。
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
Decrypt 方法要求独占访问被解密的文件,如果有其他进程正在使用该文件,此方法将引发异常。如果文件没有加密,则 Decrypt 将返回一个非零值,这表示成功。
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
Encrypt 方法和 Decrypt 方法都使用计算机上安装的加密服务提供程序 (CSP) 和调用方法的进程的文件加密密钥。
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
当前文件系统必须格式化为 NTFS,当前操作系统必须是 Microsoft Windows NT 或更高版本。
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
using System;
11
using System.IO;
12
using System.Security.AccessControl;
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
namespace FileSystemExample
15
{
16
class FileExample
17
{
18
public static void Main()
19
{
20
try
21
{
22
string FileName = "test.xml";
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
Console.WriteLine("Enceypt " + FileName);
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
// Encrypt the file.
27
AddEncryption(FileName);
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
Console.WriteLine("Decrypt " + FileName);
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
// Decrypt the file.
32
RemoveEncryption(FileName);
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
Console.WriteLine("Done");
35
}
36
catch (Exception e)
37
{
38
Console.WriteLine(e);
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
Console.ReadLine();
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
// Encrypt a file.
46
public static void AddEncryption(string FileName)
47
{
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
File.Encrypt(FileName);
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
// Decrypt a file.
54
public static void RemoveEncryption(string FileName)
55
{
56
File.Decrypt(FileName);
57
}
58
}
59
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)