How to lock a folder in C #

I want to have a persistent lock in my folder using C #.

This lock should be performed by the application only when requested. When the application is closed, this lock should not be available. Also, when the application is running, this folder cannot be moved or opened outside the application. So, only through my application should this folder be accessible.

+3
source share
4 answers

The following code will help lock and unlock the folder.

Source: http://bitsbyta.blogspot.de/2011/01/lock-and-unlock-folder-cnet.html

using System.IO;
using System.Security.AccessControl;

private void btnBrowse_Click(object sender, EventArgs e)
{

   if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
   {
        // Select the folder to lock
        textBox1.Text = folderBrowserDialog1.SelectedPath;
   }

}

private void btnLock_Click(object sender, EventArgs e)
{
  try
     {

      string folderPath = textBox1.Text;
      string adminUserName = Environment.UserName;// getting your adminUserName
      DirectorySecurity ds = Directory.GetAccessControl(folderPath);
      FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny)    
      ds.AddAccessRule(fsa);
      Directory.SetAccessControl(folderPath, ds);
      MessageBox.Show("Locked");
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }       
}

private void btnUnLock_Click(object sender, EventArgs e)
{
   try
      {
     string folderPath = textBox1.Text;
     string adminUserName = Environment.UserName;// getting your adminUserName
     DirectorySecurity ds = Directory.GetAccessControl(folderPath);
     FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName,FileSystemRights.FullControl, AccessControlType.Deny)    
     ds.RemoveAccessRule(fsa);
     Directory.SetAccessControl(folderPath, ds);
     MessageBox.Show("UnLocked");
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     } 
}
+2
source

, # , . .

, , , , . Active Directory Windows. , , , , , , - , , ( ). (, ), , .

? , , . ; -, , , Windows, , . . ( ). , , , .

+1

, , - . , , .

.

0

PInvoking CreateFile FILE_FLAG_BACKUP_SEMANTICS.

:

// Create a directory named DirToLock
const string directoryName = "DirToLock";
if (!Directory.Exists(directoryName))
{
    Directory.CreateDirectory(directoryName);
}

// Open and lock the directory we've just created
var handle = CreateFile(
    directoryName, 
    FileAccess.Read, 
    FileShare.None, // Causes all requests to open this file to fail until the file is closed.
    IntPtr.Zero, 
    FileMode.Open,
    (FileAttributes) BackupSemantics, // BackupSemantics = 0x02000000
    IntPtr.Zero
);

,

The above code will cause the directory to remain locked while the process is running. To unlock the directory while the program is running, you need to close its handle (for example, PInvoking CloseHandle ):

CloseHandle(handle);
0
source

Source: https://habr.com/ru/post/1775264/


All Articles