Click here to Skip to main content
15,886,742 members
Articles / Programming Languages / C#

Folder protection for Windows using C# and concepts on Windows Shell menu for folders

Rate me:
Please Sign up or sign in to vote.
4.07/5 (86 votes)
11 May 2012CPOL8 min read 367.9K   36K   142  
Unique folder protection that issues Windows Class IDS to protect folders.
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 Form1 : Form
    {
        public string status;
        //bool flag = true;
        string[] arr;
        private string _pathkey;
        public Form1()
        {
            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 button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                status = arr[0];
            else if (radioButton2.Checked)
                status = arr[1];
            else if (radioButton3.Checked)
                status = arr[2];
            else if (radioButton4.Checked)
                status = arr[3];
            else if (radioButton5.Checked)
                status = arr[4];
            else if (radioButton6.Checked)
                status = arr[5];

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            { 
                
                DirectoryInfo d = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                string selectedpath = d.Parent.FullName + d.Name;
                if (folderBrowserDialog1.SelectedPath.LastIndexOf(".{") == -1)
                {
                    if (checkBox1.Checked)
                        setpassword(folderBrowserDialog1.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);
                    textBox1.Text = folderBrowserDialog1.SelectedPath;
                    pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\lock.jpg");
                }
                else
                {
                    status = getstatus(status);
                    bool s=checkpassword();
                    if (s)
                    {
                        File.Delete(folderBrowserDialog1.SelectedPath + "\\p.xml");
                        d.MoveTo(folderBrowserDialog1.SelectedPath.Substring(0, folderBrowserDialog1.SelectedPath.LastIndexOf(".")));
                        textBox1.Text = folderBrowserDialog1.SelectedPath.Substring(0, folderBrowserDialog1.SelectedPath.LastIndexOf("."));
                        pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\unlock.jpg");
                    }
                }
            }
        }
        
        private bool checkpassword()
        {
            XmlTextReader read;
            if(pathkey ==null)
            read = new XmlTextReader(folderBrowserDialog1.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)
        {
            password p = new password();
            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)
                    {
                        textBox1.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);
                        pictureBox1.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(".")));
                        textBox1.Text = pathkey.Substring(0, pathkey.LastIndexOf("."));
                        pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\unlock.jpg");
                    }
                }
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
President
India India
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.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

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.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions