Pages

Thursday 24 April 2014

Lock a Folder in C#


Hi,

Here you will learn how to lock and Unlock folders using C#. This tutorial is of good use if you are planning to create a Folder Locking Software. 


Let's start;


Steps

1.Open Visual Studio -> New Project -> Windows Form Application with name 
   FolderLock.

2.Now add buttons, label, textbox and folderBrowserDialog to your blank form and 
   design it as shown in the below screenshot.




3.Rename the controls as given below; (Click on the buttons & textbox and change the
  Text and Name from the Properties dialog box)

     Text         Name
     Lock    -    btnLock
     Unlock -    btnUnlock
     Browse-    btnBrowse
     textbox1-  txtFilePath

4. Now double click on Browse button and copy the below code;
   
     if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = folderBrowserDialog1.SelectedPath;
            }

5. Now double click on Lock button and copy the below code;

 if (txtFilePath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtFilePath.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
                {
                }
            }

    
6. Also double click on Unlock button and copy the below code;
     
           if (txtFilePath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtFilePath.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
                {
                }
            }


7. Now your program code window (Form1.cs) looks like the below one.

Form1.cs

using System;
using System.IO;
using System.Security.AccessControl;
using System.Windows.Forms;

namespace FolderLock
{
    public partial class FolderLock : Form
    {
        public FolderLock()
        {
            InitializeComponent();
        }


        private void btnLock_Click(object sender, EventArgs e)
        {
            if (txtFilePath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtFilePath.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
                {
                }
            }

        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            if (txtFilePath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtFilePath.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
                {
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}

8. Now Run your project and Browse a folder. The folder path will be shown in the
   textbox. Now click on Lock button, the folder will be locked and you will get a message
   ( see the screenshot below). Check if you can open the locked folder, No is it? Now    
   click on Unlock and try again. So you have created a locking software. Enjoy :-)

    Output



Conclusion

If you have got any errors / doubts, feel free to comment it.

See Also




10 comments:

  1. Thanks for this informative post...

    ReplyDelete
  2. Wow I just learned to create Folder Locking S/W Thnks a lot..

    ReplyDelete
  3. Very Good man, helped a lot. Thanks.

    ReplyDelete
  4. hey, Please help me.

    I am developing a window application of screen capturing for my company use. I want capture the screen shoot and store those screen shot automatically in lock folder by programming. And after storing, i want to upload those screen shot on my company's server from that lock folder. so, I don't to perform operation like delete, copy, paste on that folder. My problem is that, when I write Fullcontrol then I am not able to store that screen shot. So, Please tell me, how to do this? Which kind of access i have to give?

    ReplyDelete
  5. hey, Please help me.

    I am developing a window application of screen capturing for my company use. I want capture the screen shoot and store those screen shot automatically in lock folder by programming. And after storing, i want to upload those screen shot on my company's server from that lock folder. so, I don't to perform operation like delete, copy, paste on that folder. My problem is that, when I write Fullcontrol then I am not able to store that screen shot. So, Please tell me, how to do this? Which kind of access i have to give?

    ReplyDelete
  6. I am not able to lock the folder. The message box says "locked", but I can still open the folder. Any help will be appriciated.

    ReplyDelete
  7. Why on USB drive not work?

    ReplyDelete
  8. wowwwwwwwwwwww its just amazing thanksss it will help me alot for my demo :) keep it up

    ReplyDelete
  9. its just awsome thanks alot for that it will help me as demo for my final year project hats off

    ReplyDelete

 

About Me

I am Tijo Tom, a Computer Engineer from India.Right from my schooling, I had a passion for Programming, PC Tricks, Virus Creation and Hacking.

I started this blog on Jan 1, 2014 to share my views and ideas with the world.

You can know more about me on