jnrdelta.blogg.se

How to lock a folder in windows
How to lock a folder in windows




how to lock a folder in windows

This allows teams and collaborators to coordinate who is editing a file and avoid conflicted copies. Finally, you're much less likely to end up with a bug and field complaints that your program has created an inaccessible, space-wasting folder on a user's computer that they can't remove even after uninstalling the program.File locking is a feature that prevents a file from being edited. DBs also allow very fine granularity over data access (down to the column level). Programmatic administration of folder rights also requires giving your program administrative privileges. I say this for several reasons first, it's much easier to connect to a DB as a different user than it is for a program to impersonate a different Windows user than the one running the program. What will this file contain? If you're looking to store sensitive data, or data that should not be changed, I would put it in a DB. Then, have your program authenticate itself as that user (probably by impersonation), and it will be able to use the folder, but others won't. Keep in mind that you will usually want to let Administrators in too, so if something goes wrong you can fix it without having to be this new user (who may not have other needed permissions to fix the problem). This can be done with Active Directory or plain ol' Windows user accounts. What you could do is set up a user who is the owner of that folder, and the only one that can read/write to it. A program can lock a resource only while it's running. In short, C# alone won't get you what you want. Private void btnUnLock_Click(object sender, EventArgs e)įileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName,FileSystemRights.FullControl, AccessControlType.Deny)

how to lock a folder in windows

String adminUserName = Environment.UserName // getting your adminUserNameĭirectorySecurity ds = Directory.GetAccessControl(folderPath) įileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny)ĭirectory.SetAccessControl(folderPath, ds) Private void btnLock_Click(object sender, EventArgs e) TextBox1.Text = folderBrowserDialog1.SelectedPath If (folderBrowserDialog1.ShowDialog() = DialogResult.OK) Private void btnBrowse_Click(object sender, EventArgs e) The Following code will help to lock and unlock the folder.






How to lock a folder in windows