Creating a new directory from C# with Encrypting File System switched on

≯℡__Kan透↙ 提交于 2019-11-30 05:31:21

问题


Has anyone created a new directory from C# with Encrypting File System switched on?

Additionally any information on doing this from an install would be helpful too.


回答1:


Creating an encrypted directory would be a two step process - create it using Directory.CreateDirectory and then encrypt it using the Win32 function EncryptFile. Sample code -

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace EncryptDir
{
    public class Sample
    {
        DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool EncryptFile(string filename);

        public static void Main ()
        {
            Directory.CreateDirectory("MyEncryptedDirectory");
            EncryptFile("MyEncryptedDirectory");
        }
}

References:
EncryptFile Function @ MSDN
Handling encrypted files and directories @ MSDN




回答2:


It seems that calling File.EncryptFile also works on directories. I guess it just forwards to EncryptFile internally.




回答3:


The managed methods File.Encrypt() and FileInfo.Encrypt() both simply call the native EncryptFile() that is shown in the other answer.

So, no need to go to all the trouble to declare the p/invoke API. Just use the built-in methods.



来源:https://stackoverflow.com/questions/6111918/creating-a-new-directory-from-c-sharp-with-encrypting-file-system-switched-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!