![]() |
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 FoldersBy Abhishek SurUnique 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
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
//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:

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:

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:

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.
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.

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.
To make shell context menu called Unique Folder protection, I have made a registry entry as

HKEY_CLASSES_ROOT\Folder\Shell\Unique Folder Lock\command
[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.
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:
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.
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:
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
Download coolcode_demo2.zip - 470.9 KB
I have also added the software to Windows Shell Menu. Take a look at the snapshot below:
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
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.
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
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.
General
News
Question
Answer
Joke
Rant
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 |