+ Reply to Thread
Results 1 to 4 of 4

Thread: how to lock folders in c# ?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    19

    how to lock folders in c# ?

    i want to know the method of locking and un-locking folders in c# , i`ll need the access-control method

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    34
    Code:
            private void lockfolder(string path)
            {
                try
                {
                    if (path == "")
                    {
                        MessageBox.Show("Empty Path , please choose a path", "Error 0X001", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DirectoryInfo dir = new DirectoryInfo(path);
    
                        foreach (FileInfo filesindires in dir.GetFiles()) // getting files in directory to lock them
                        {
                            if (filesindires.FullName != Application.ExecutablePath) // this to prevent locking the main program as well 
                            {
                                FileSecurity ds = filesindires.GetAccessControl();
                                ds.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Deny));
                                filesindires.SetAccessControl(ds);
                                Thread.Sleep(5); // this in order not to make CPU busy all the time
                            }
                        }
    
                        foreach (DirectoryInfo dirsinfolder in dir.GetDirectories()) // recursive method if directory is found
                        {
                            lockfolder(dirsinfolder.FullName);
                        }
                    } 
                }
                catch
                {
                    MessageBox.Show("An error occured , please report it to me", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            private void unlockfolder(string path)
            {
                try
                {
                    if (path == "")
                    {
                        MessageBox.Show("Empty Path , please choose a path", "Error 0X001", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DirectoryInfo dir = new DirectoryInfo(path);
    
                        foreach (FileInfo filesindires in dir.GetFiles())
                        {
                            if (filesindires.FullName != Application.ExecutablePath) // this to prevent locking the main program as it will be stupid
                            {
                                FileSecurity ds = filesindires.GetAccessControl();
                                ds.RemoveAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Deny));
                                filesindires.SetAccessControl(ds);
                                Thread.Sleep(5);
                            }
                        }
    
                        foreach (DirectoryInfo dirsinfolder in dir.GetDirectories())
                        {
                            unlockfolder(dirsinfolder.FullName);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("An error occured , please report it to me", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    have fun , hope it helps .

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    19
    thank you very much , it helped indeed .

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    34
    any time

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts