Click here to Skip to main content
6,630,289 members and growing! (20,981 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » General     Intermediate License: The Code Project Open License (CPOL)

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

By Abhishek Sur

Unique folder protection that issues Windows Class IDS to protect folders
VB, C# 2.0, Windows, .NET 2.0, Visual Studio, CEO, Architect, DBA, Dev, Design
Posted:15 Oct 2007
Updated:2 Jan 2008
Views:42,190
Bookmarked:53 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 4.86 Rating: 3.40 out of 5
5 votes, 18.5%
1
1 vote, 3.7%
2
1 vote, 3.7%
3
2 votes, 7.4%
4
18 votes, 66.7%
5

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


Member
The guy is doing programming since 2001. Started his career with C, then C++ and so on. He completed his Masters in Computers from JIS, Kolkata. He likes to know the unknown. Now he is doing job as .NET Developer in a MNC. If you want to add him as buddy... Just click here... Or Directly Send Messages

To email abhi2434@yahoo.com
He is now working in a US Based Company in Kolkata.

His WebSite
Home page

Technical Blog
Programming Help and Tricks
Hidden Tips on Windows
.NET Ideas(My Previous Blog)

Most Importantly, He uses Orkut very often add him if you want

If you like this, Try reading some more:
Articles Listing
Description never ends, lots of secret with him. Make him your friend to learn more.

Dont forget to vote or share your comments about his Writing

Thanks in advance.
Occupation: Web Developer
Company: Buildfusion Inc
Location: India India

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
GeneralIt seems nice Pinmemberranjan_namitaputra12:09 2 Oct '09  
GeneralUnsatisfied PinmemberShri_C22:24 13 Sep '09  
GeneralRe: Unsatisfied PinassociateAbhishek Sur22:38 13 Sep '09  
Generallock folder project Pinmemberottilia7:53 9 Sep '09  
GeneralGood Concept - Bad Implementation Pinmembersans sans sans5:13 28 Aug '09  
Generalhow to completely invisible a folder? Pinmemberkhoirom16:51 13 Jul '09  
General??? Pinmemberehtesham0157012:58 22 Jun '09  
GeneralRe: ??? PinmemberAbhishek Sur0:04 24 Jun '09  
GeneralThanks Pinmembersonia.sardana23:35 8 Jun '09  
GeneralRe: Thanks PinmemberAbhishek Sur13:54 10 Jun '09  
GeneralRe: Thanks PinmemberDineshSIS0:36 19 Jun '09  
QuestionA Bug Pinmemberhmnmsd7:04 28 Nov '08  
AnswerRe: A Bug PinmemberAbhishek Sur21:39 9 Dec '08  
GeneralIt is not working [modified] Pinmemberprasadtalla2:53 15 May '08  
QuestionRe: It is not working PinmemberAbhishek sur10:20 17 May '08  
AnswerRe: It is not working Pinmemberprasadtalla22:23 18 May '08  
AnswerRe: It is not working PinmemberAbhishek sur0:36 18 Jun '08  
GeneralThere is no source code for shell integration PinmemberMember 403689219:47 19 Mar '08  
GeneralRe: There is no source code for shell integration PinmemberAbhishek sur22:13 19 Mar '08  
GeneralRe: This is simply sick Pinmemberring_023:02 2 Jan '08  
GeneralRe: This is simply sick PinmemberAbhishek sur23:34 2 Jan '08  
General... Pinmemberb4ckd00r19:37 22 Oct '07  
General... Pinmemberb4ckd00r19:34 22 Oct '07  
AnswerRe: ... PinmemberAbhishek sur21:03 22 Oct '07  
Questionfolder lost Pinmemberb4ckd00r13:30 17 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jan 2008
Editor:
Copyright 2007 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project