Click here to Skip to main content
Licence CPOL
First Posted 15 Oct 2007
Views 91,187
Downloads 6,179
Bookmarked 94 times

Folder protection for windows using C# & Concepts on Windows Shell menu for Folders

Unique folder protection that issues Windows Class IDS to protect folders
7 votes, 16.3%
1
1 vote, 2.3%
2
2 votes, 4.7%
3
5 votes, 11.6%
4
28 votes, 65.1%
5
4.56/5 - 43 votes
7 removed
μ 3.76, σa 2.68 [?]

UNIQUE FOLDER PROTECTION


Introduction

This Article is based on concept that will make you use your own windows platform in somewhat a better way. I am introducing a new concept of folder locking.

Generally, for everyone, it is a common topic to know how to lock and secure your folders from others. This issue is really burns lots of heads. Now, to make you understand the issue of locking your folders in windows, I am presenting the article. I think everyone will appreciate my post.

Background

The article can be used by novice, because it is totally build for users who dont know much about computers. This project uses Class IDS that can be found in registry to identify programs, and I will associate those classids to the folders to make them hidden from the others.

Using the code

To use the code, you must know .NET. I am using C# to make my project. It is windows based application made in c#. The logic of taking the default names as of windows(like textbox1,Radiobutton1 etc) to make this project simple to make. If anyone wants an updation, I will definitely place the names accordingly.
//Form 1 of Unique folder protection
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace FolderProtection
{
    public partial class frmMain : Form
    {
        public string status;
        //bool flag = true;
        string[] arr;
        private string _pathkey;
        public frmMain()
        {
            InitializeComponent();
            arr = new string[6];
            status = "";
            arr[0] = ".{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}";
            arr[1] = ".{21EC2020-3AEA-1069-A2DD-08002B30309D}";
            arr[2] = ".{2559a1f4-21d7-11d4-bdaf-00c04f60b9f0}";
            arr[3] = ".{645FF040-5081-101B-9F08-00AA002F954E}";
            arr[4] = ".{2559a1f1-21d7-11d4-bdaf-00c04f60b9f0}";
            arr[5] = ".{7007ACC7-3202-11D1-AAD2-00805FC1270E}";
        }
        public string pathkey
        {
            get { return _pathkey; }
            set { _pathkey=value; }
        }
         private void btnSet_Click(object sender, EventArgs e)
        {
            if (rWLock.Checked)
                status = arr[0];
            else if (rCtrlPan.Checked)
                status = arr[1];
            else if (rWebBrow.Checked)
                status = arr[2];
            else if (rRclbin.Checked)
                status = arr[3];
            else if (rHlpSprt.Checked)
                status = arr[4];
            else if (rNetw.Checked)
                status = arr[5];

            if (fldrDialog.ShowDialog() == DialogResult.OK)
            { 
                
                DirectoryInfo d = new DirectoryInfo(fldrDialog.SelectedPath);
                string selectedpath = d.Parent.FullName + d.Name;
                if (fldrDialog.SelectedPath.LastIndexOf(".{") == -1)
                {
                    if (chkPasswd.Checked)
                        setpassword(fldrDialog.SelectedPath);
                    if (!d.Root.Equals(d.Parent.FullName))
                    d.MoveTo(d.Parent.FullName + "\\" + d.Name + status);
                    else d.MoveTo(d.Parent.FullName + d.Name + status);
                    txtPath.Text = fldrDialog.SelectedPath;
                    picStat.Image = Image.FromFile(Application.StartupPath + "\\lock.jpg");
                }
                else
                {
                    status = getstatus(status);
                    bool s=checkpassword();
                    if (s)
                    {
                        File.Delete(fldrDialog.SelectedPath + "\\p.xml");
                        d.MoveTo(fldrDialog.SelectedPath.Substring(0, fldrDialog.SelectedPath.LastIndexOf(".")));
                        txtPath.Text = fldrDialog.SelectedPath.Substring(0, fldrDialog.SelectedPath.LastIndexOf("."));
                        picStat.Image = Image.FromFile(Application.StartupPath + "\\unlock.jpg");
                    }
                }
            }
        }
        
        private bool checkpassword()
        {
            XmlTextReader read;
            if(pathkey ==null)
            read = new XmlTextReader(fldrDialog.SelectedPath + "\\p.xml");
            else
            read = new XmlTextReader(pathkey + "\\p.xml");
            if (read.ReadState == ReadState.Error)
                return true;
            else
            {
                try
                {
                    while (read.Read())
                        if (read.NodeType == XmlNodeType.Text)
                        {
                            checkpassword c = new checkpassword();
                            c.pass = read.Value;
                            if (c.ShowDialog() == DialogResult.OK)
                            {
                                read.Close();
                                return c.status;
                            }

                        }
                }
                catch { return true; }
                
            }
            read.Close();
            return false;
        }

        private Boolean setpassword(string path)
        {
            frmPassword p = new frmPassword();
            p.path = path;
            p.ShowDialog();
            return true;
        }
        private string getstatus(string stat)
        {
            for (int i = 0; i < 6; i++)
                if (stat.LastIndexOf(arr[i]) != -1)
                    stat = stat.Substring(stat.LastIndexOf("."));
            return stat;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (this.pathkey != null)
            {
                
                    DirectoryInfo d = new DirectoryInfo(pathkey);
                    string selectedpath = d.Parent.FullName + d.Name;
                    if (pathkey.LastIndexOf(".{") == -1)
                    {
                        txtPath.Text=pathkey;
                        DialogResult r;
                        r = MessageBox.Show("Do You want to set password ? ", "Question?", MessageBoxButtons.YesNo);
                        if (r == DialogResult.Yes)
                        {
                            setpassword(pathkey);
                        }
                        status = arr[0];
                        if (!d.Root.Equals(d.Parent.FullName))
                            d.MoveTo(d.Parent.FullName + "\\" + d.Name + status);
                        else d.MoveTo(d.Parent.FullName + d.Name + status);
                        picStat.Image = Image.FromFile(Application.StartupPath + "\\lock.jpg");
                    }
                    else
                    {
                    status = getstatus(status);
                    bool s=checkpassword();
                    if (s)
                    {
                        File.Delete(pathkey + "\\p.xml");
                        d.MoveTo(pathkey.Substring(0, pathkey.LastIndexOf(".")));
                        txtPath.Text = pathkey.Substring(0, pathkey.LastIndexOf("."));
                        picStat.Image = Image.FromFile(Application.StartupPath + "\\unlock.jpg");
                    }
                }
            }
        }

    }
}
 


The above code is placed in form 1 of the current project. To do this project first of all, start an application choosing windows application, and place some radiobuttons on the form. Those radiobuttons will lock your folder appropriately to their respective class ids. The classids are automatically registering components that are installed on any windows system. You can get classids from windows registry. Find some help regarding CLSIDs from my bolg at http://windowstricks.spaces.live.com.

This form have a checkbox called set password, which is checked automatically. If you want to disable the locking feature to lock your folder, just uncheck this one.

Next to this, choosing the folder from the button, it will open the dialogbox choose your folder, and it will be locked.

To describe the coding, after the form is been opened if, it will assign the appropriate classids to an array called arr. Whenever the used clicks the button, it will open the file browser dialog box. Based on the radiobutton the clsid will be assigned to a variable called status. After choosing the appropriate folder, if there it is not already locked, it will lock it by renaming the folder with the extension of the classids. So that the folder will not open anymore.

Take a look at the snapshots:

Screenshot - cool_image4.jpg

The set password function will open up a new form called password, to set the password. To set the password, I have used a technique. I have made an xml file just within the folder and named it as p.xml. If the folder already have this file, it will be overwritten. so please make sure that there is no file within the folder named as p.xml.

To unlock the folder you have to again choose the folder using folderdialog box, in this case, I have called a function called getstatus, which will browse through all the clsids and choose the appropriate one from them. And then it will rename the folder accordingly. Have a look at the snapshot below:

Screenshot - cool_image2.jpg

If the folder is locked using a password, it will open up a new form called checkpassword. This form will read the xml file within the folder and check with the password given on the textbox with it. If it is correct, the function will return true, otherwise it will return false. The code of this form will look like:

//Check password dialog box...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FolderProtection
{
    public partial class checkpassword : Form
    {
        public string pass;
        public bool status;
        public checkpassword()
        {
            status = false;
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text.Equals(pass))
            {
                status = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("Incorrect Password!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = false;
            }
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

The password is set automatically from form1 to a variable called pass. It will check both of them and send the boolean value to the caller. Take a look at the snapshot below:

Screenshot - cool_image3.jpg

To set the password I have used another form called password. It will create the xml file within the folder. Take a look at the code of that form :

//Password Entry dialog box... 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace FolderProtection
{
    public partial class frmPassword : Form
    {
        public string path;
        public frmPassword()
        {
            InitializeComponent();
     
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text.Equals(txtReenter.Text) && txtPassword.Text.Length !=0 )
            {
                XmlDocument xmldoc = new XmlDocument();
                XmlElement xmlelem;
                XmlNode xmlnode;
                XmlText xmltext;
                xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmldoc.AppendChild(xmlnode);
                xmlelem = xmldoc.CreateElement("", "ROOT", "");
                xmltext = xmldoc.CreateTextNode(txtPassword.Text);
                xmlelem.AppendChild(xmltext);
                xmldoc.AppendChild(xmlelem);
                xmldoc.Save(path + "\\p.xml");
                this.Close();
            }
            else
            {
                MessageBox.Show("Two text do not match or Blank Password", "Error");
                txtPassword.Clear();
                txtReenter.Clear();
                txtPassword.Focus();
            }
        }

    }
}

This form will create a file on the same location as the path set from form1 and read the password and set write in the root tag.

To unlock the folder choose the already locked folder and the picturebox will assure you if it is successfully unlocked or not. The successful unlock will load the picture below.

cool_image9.JPG

In the program.cs we made the part which will be included for the context menu.Check out the code below:

//Program.cs first to open... 
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace FolderProtection
{
    static class Program
    {
        /// <summary>
        ///Application will Check if it is opened 
        ///from Context menu or not and place the path accordingly
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
           
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmMain f = new frmMain();
            if (args.Length > 0)
                f.pathkey = args[0];
            Application.Run(f);
        }
    }
}

In this code I have assigned the pathkey if there is any argument comes from the command line of main. In general, if I open the software from run menu, there will not be any argument passed as an argument if we not explicitely send it from there. This arguments will come handy in case of using the shell context menu. Whenever we open this software from the context menu of folders, it will take the path in this arguments. For that I needed to include some Registry entries. check out the images below.

RUN MENU OPTION:

Screenshot - cool_image7.jpg
In the above image it is shown that while creating the installer, I have made a registry key in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\lock.exe

This key will be used to create an option in run menu. The string path is taken as [TARGETDIR]FolderProtection.exe, so when I call lock.exe from run menu, it will be calling the file installed in your machine. TARGETDIR will give you the exact installation folder chosen.

SHELL CONTEXT MENU:

To make shell context menu called Unique Folder protection, I have made a registry entry as

Screenshot - cool_image8.jpg

HKEY_CLASSES_ROOT\Folder\Shell\Unique Folder Lock\command

<>this will create a new option for right click on any folder as unique folder lock. After that, in the command, it will open [TARGETDIR]Folderprotection.exe %1, so while we choose this option, it will open up the exe, The %1 defines that when it opens up the software, it will take the path of the folder chosen as argument. The argument will be taken to pathkey for use in program.cs.>

The complete source can be downloaded from the link below:

Download coolcode_src1.zip - 56.1 KB

Open the solution and you will get everything within it.

Instruction to use this Software:

For Novice, it is very handy to use this software. Please download the installer from the link below and install it according to the instruction given below:

First Download the software from the link:

Download coolcode_demo1.zip - 427.3 KB

To install this software, please run "Protection.msi". This will ask for components, which may require Internet connection sometimes.

Next after installation procedure is over, you will not find any icon over the desktop nor you will get any link in start menu.

To Run this application:

Go to START-> RUN, then type Lock.exe. The application will open up and you can use it.

Have a look at the snapshot below:

Screenshot - cool_image5.jpg

Note: To Lock or unlock, just use start - > run to open the software. you will not find any link to the programs from the start menu

Update to Installer

Download coolcode_demo2.zip - 470.9 KB

I have also added the software to Windows Shell Menu. Take a look at the snapshot below:

Screenshot - cool_image6.jpg

To Open the software you only need to go to "Unique Folder Lock" from the menu.. Try downloading this link for this option Please..

Download coolcode_demo2.zip - 470.9 KB

NEXT UPDATE:

In my early installer, it just opens the software, where you need to choose the folder again. In this update, it will dynamically lock or unlock your folders. Check out the new link below:

Download coolcode_demo3.zip - 471.8 KB

Download coolcode_src3.zip - 55.6 KB

Points of Interest

This project uses the windows registered classids. you can get your own from registry. I have made this software for Windows, So it will not run in any other platform, also some of the clsids may not work in your computer. If there is any case, I am sure, you can make changes to this software. But for novice, try using the options called Control Panel or Recycle Bin, if it is other than Windows XP or later. I think you can definately find this software handy.

If you want more help on clsid or this software, you can look at my blog at http://windowstricks.spaces.live.com, or feel free to write me at abhishek.sur@gmail.com or abhi2434@yahoo.com. Thank you.

History

UPDATE 1 : This is my first update. Hoping for positive replies to this article. I need your appreciation friends. Also let me know if the software is bug free or not.

Thank you very much for reading this article.

UPDATE PROJECT LINKS :

Download coolcode_src.zip - 56.1 KB

Download coolcode_demo.zip - 427.3 KB

UPDATE 2:
In this update, I have made some changes to the installer. Check out.
UPDATE PROJECT LINKS :
Download coolcode_demo1.zip - 427.3 KB

UPDATE 3:In this update I have added the context menu part of the installer, so that one may click on the context menu of windows shell to choose a folder.
UPDATE PROJECT LINKS :
Download coolcode_demo2.zip - 470.9 KB

Download coolcode_src1.zip - 56.1 KB

UPDATE 4:

In this update, I have modified the project so that some of the bugs of my early release could be removed. Now you can dynamically choose any folder from your harddrive and the software will ask for passwords.

UPDATE PROJECT LINKS :

Download coolcode_src3.zip - 55.6 KB

Download coolcode_demo3.zip - 471.8 KB

Please respond me how you see my article. Future modification is due.

Thanks for Reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur

Web Developer
Buildfusion Inc
India India

Member

Follow on Twitter Follow on Twitter
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAllows to view and copy files from command line Pinmemberkrravisha18:25 31 Jan '12  
Questionabt project PinmemberSantosh Selokar3:19 9 Sep '11  
QuestionGreat concept Pinmemberjofrfeezy9:24 14 Aug '11  
AnswerRe: Great concept PinmvpAbhishek Sur10:45 15 Aug '11  
Questionopen and view folder content in a program aaha PinmemberTassenmann9:33 5 Aug '11  
GeneralMy vote of 4 Pinmembervk_ravikumar8:39 10 Jun '11  
Generalpathkey Pinmemberreygie3:13 28 Feb '11  
GeneralMy vote of 3 Pinmembersivakumarmr103:26 20 Jan '11  
Generalnot working in windows xp , but it working in 7 Pinmembersivakumarmr103:20 20 Jan '11  
GeneralHi Pinmembersinghishu1:46 14 Oct '10  
GeneralMy vote of 1 PinmemberRakesh Garia22:55 13 Sep '10  
GeneralMy vote of 1 PinmemberSyed Shakeer Hussain21:50 4 Sep '10  
GeneralNot work for folder on usb Pinmembertmthuan1231:25 13 Aug '10  
QuestionWhy program is not working ? Pinmembertomluckose20:30 23 Apr '10  
GeneralNice work, but rename the folder in dos cracks the lock.. PinmemberRakesh_Sharma2323:56 26 Mar '10  
QuestionCan we also disable deleting of the Locked Folders PinmemberCoolMahen22:26 14 Mar '10  
Generalfolder protection Pinmembersujibharathi0:43 17 Feb '10  
GeneralRe: folder protection PinmvpAbhishek Sur6:55 17 Feb '10  
Generalhey Pinmemberkadrija8:10 10 Feb '10  
Generalforget the password PinmemberHemant02622:44 4 Feb '10  
GeneralRe: forget the password PinmvpAbhishek Sur23:34 4 Feb '10  
Generalproblem Pinmemberspider_vikas20:08 4 Jan '10  
Generalthanks code.. Pinmembermk.developer23:11 29 Dec '09  
GeneralRe: thanks code.. PinmvpAbhishek Sur0:15 12 Jan '10  
GeneralVery interesting PinmemberRitesh Ramesh8:27 17 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 3 Jan 2008
Article Copyright 2007 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid