Creating a new C # directory with encryption enabled

Has anyone created a new directory from C # with encryption enabled?

In addition, any information about this during installation will be useful.

+5
source share
3 answers

Creating an encrypted directory will be a two-step process - create it using Directory.CreateDirectory, and then encrypt it using the Win32 EncryptFile function. Code Example -

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 @MSDN Function
Processing encrypted files and @MSDN directories

+6
source

, File.EncryptFile . , EncryptFile .

0

Managed methods File.Encrypt()and FileInfo.Encrypt()both just call native EncryptFile(), which is shown in another answer .

This way you don't have to worry about declaring the p / invoke API. Just use the builtin methods.

0
source

All Articles