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.
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); } }
, # , . .
, , , , . Active Directory Windows. , , , , , , - , , ( ). (, ), , .
? , , . ; -, , , Windows, , . . ( ). , , , .
, , - . , , .
.
PInvoking CreateFile FILE_FLAG_BACKUP_SEMANTICS.
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);
Source: https://habr.com/ru/post/1775264/More articles:ΠΠ°ΠΊ ΡΠ²Π΅Π»ΠΈΡΠΈΡΡ ΡΠΊΠΎΡΠΎΡΡΡ ΠΏΠΎΠΈΡΠΊΠ° Π² Iphone SDK - objective-cΠΠΎΠ·ΠΌΠΎΠΆΠ½Π° Π»ΠΈ ΠΌΠ°ΡΠΆΠ° Π΄Π»ΡΠ² css? - htmlΠΠΎΠ»ΡΡΠΈΡΡ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡΡ ΠΈΠ· Π½Π΅ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎΠΉ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ - javaVisual Studio 2010 - new tabbed window - visual-studio-2010how to know when a c ++ program is waiting for input? - c ++Debug Battery Warning - debuggingConfuses the use of ContextLoaderListener in Spring MVC - javaStop method and wait for user input - multithreadingYou must create a search syntax in gmail format; perhaps using regular expressions? - regexHow can I avoid redundancy when declaring new class attributes in Objective-C? - propertiesAll Articles