locked
Lock a Folder RRS feed

  • Question

  • hi
    I am trying to make a program that will lock a folder of my choice. ( and later unlock it ). the locked folder will also change its Icon....  I have used the search option, but it didnt gave me nothing interesting to start... so can anybody help me?

    Tuesday, November 20, 2007 6:21 PM

Answers

  • Hi mNero,

     

    Please check this document including demo.

    Unique folder protection that issues Windows Class IDS to protect folders

     

    Based on the above document, I create the following code sample to lock specified folder and change folder icon.

     

    Main idea: Use Class IDS that can be found in registry to identify programs, and associate those classids to the folders to make them hidden from the others.

     

    For example, when I lock a folder named Email, its name will change to Email.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0} and the folder icon will change to a lock icon. Please take it a try.

     

    Prerequisites: FolderBrowserDialog1, TextBox1 and Button1 on Form1.

    Code Block

    Imports System.IO

    Imports System.Drawing

    Imports System.Windows.Forms

     

    Public Class Form1

     

        Public status As String

        Private arr As String() = New String(5) {}

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            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}"

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            status = arr(0)

     

            If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

     

                Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

                Dim selectedpath As String = d.Parent.FullName + d.Name

     

                If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

     

                    If (Not d.Root.Equals(d.Parent.FullName)) Then

                        d.MoveTo(d.Parent.FullName & "\" & d.Name & status)

                    Else

                        d.MoveTo(d.Parent.FullName + d.Name & status)

                    End If

                    textBox1.Text = FolderBrowserDialog1.SelectedPath

                    ' PictureBox1.Image = Image.FromFile(Application.StartupPath & "\lock.jpg")

                Else

                    status = getstatus(status)

     

                    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")

     

                End If

            End If

        End Sub

     

        Private Function getstatus(ByVal stat As String) As String

            For i As Integer = 0 To 5

                If stat.LastIndexOf(arr(i)) <> -1 Then

                    stat = stat.Substring(stat.LastIndexOf("."))

                End If

            Next i

            Return stat

        End Function

     

    End Class

     

     

    Regards,

    Martin

    Wednesday, November 21, 2007 6:44 AM
  • Hi mNero,

     

    The code you want is as below:

    Prerequisites: FolderBrowserDialog1, ListBox1, LockButton and UnLockButton on Form1.

    Code Block

    Imports System.IO

    Public Class Form1

        Public status As String

        Private arr As String() = New String(5) {}

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            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}"

        End Sub

      

        ' Lock

        Private Sub LockButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LockButton.Click

            status = arr(0)

            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then

                Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

                Dim selectedpath As String = d.Parent.FullName + d.Name

                If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

                    If (Not d.Root.Equals(d.Parent.FullName)) Then

                        Dim newPath As String = d.Parent.FullName & d.Name & status

                        d.MoveTo(newPath)

                        Me.ListBox1.Items.Add(newPath)

                    End If

                End If

            End If

        End Sub

     

        ' UnLock

        Private Sub UnLockButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnLockButton.Click

            Try

                status = arr(0)

                Dim d As DirectoryInfo = New DirectoryInfo(ListBox1.SelectedItem.ToString)

                Dim selectedpath As String = d.Parent.FullName + d.Name

                Dim lockPath As String = ListBox1.SelectedItem.ToString

                If lockPath.LastIndexOf(".{") <> -1 Then

                    status = getstatus(status)

                    d.MoveTo(lockPath.Substring(0, lockPath.LastIndexOf(".")))

                    ListBox1.Items.Remove(ListBox1.SelectedItem)

                End If

            Catch ee As Exception

                MsgBox(ee.Message)

            End Try

        End Sub

       

        Private Function getstatus(ByVal stat As String) As String

            For i As Integer = 0 To 5

                If stat.LastIndexOf(arr(i)) <> -1 Then

                    stat = stat.Substring(stat.LastIndexOf("."))

                End If

            Next i

            Return stat

        End Function

     

    End Class

     

    Tuesday, November 27, 2007 3:23 AM

All replies

  • Hi mNero,

     

    Please check this document including demo.

    Unique folder protection that issues Windows Class IDS to protect folders

     

    Based on the above document, I create the following code sample to lock specified folder and change folder icon.

     

    Main idea: Use Class IDS that can be found in registry to identify programs, and associate those classids to the folders to make them hidden from the others.

     

    For example, when I lock a folder named Email, its name will change to Email.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0} and the folder icon will change to a lock icon. Please take it a try.

     

    Prerequisites: FolderBrowserDialog1, TextBox1 and Button1 on Form1.

    Code Block

    Imports System.IO

    Imports System.Drawing

    Imports System.Windows.Forms

     

    Public Class Form1

     

        Public status As String

        Private arr As String() = New String(5) {}

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            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}"

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            status = arr(0)

     

            If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

     

                Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

                Dim selectedpath As String = d.Parent.FullName + d.Name

     

                If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

     

                    If (Not d.Root.Equals(d.Parent.FullName)) Then

                        d.MoveTo(d.Parent.FullName & "\" & d.Name & status)

                    Else

                        d.MoveTo(d.Parent.FullName + d.Name & status)

                    End If

                    textBox1.Text = FolderBrowserDialog1.SelectedPath

                    ' PictureBox1.Image = Image.FromFile(Application.StartupPath & "\lock.jpg")

                Else

                    status = getstatus(status)

     

                    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")

     

                End If

            End If

        End Sub

     

        Private Function getstatus(ByVal stat As String) As String

            For i As Integer = 0 To 5

                If stat.LastIndexOf(arr(i)) <> -1 Then

                    stat = stat.Substring(stat.LastIndexOf("."))

                End If

            Next i

            Return stat

        End Function

     

    End Class

     

     

    Regards,

    Martin

    Wednesday, November 21, 2007 6:44 AM
  • wwwaaaawwwww!!!  works super!!!  Thank you!
    I will buy you a drink!
    Wednesday, November 21, 2007 9:47 AM
  • You're welcome!

     

    Thank you for your quick feedback and active participation in MSDN forums.

     

    If you have any difficulties in future programming, please feel free to come back.

     

     

    Have a nice day.

     

    Martin

    Wednesday, November 21, 2007 10:04 AM
  • This works perfectly! Thanks for posting that. Just what I was looking for. I do have notes to mention as Im using VB 2005.

     

    I commented out the following imports without side effects.

    'Imports System.Drawing

    'Imports System.Windows.Forms

     

    I do have a couple of questions.

     

    1) Running under Vista the lock icon is the old one from XP (Shell32.dll,-48). Is there anyway at run time to specify a different icon? Or would I have to make a system wide change that would affect all other applications using that classID?

     

    2) Is it possible to 'lock' a file just like a folder? What modifications would have to be made?

     

    Again great work and well appreciated!

    Sunday, November 25, 2007 10:12 AM
  • Martin...Iam trying to change a little your code... without success

    i want two buttons...one for Lock and one for unlock a folder.

    when a folder is locked i want its filepath to be added to a listbox and then when i unlock it, I simply select a filepath from the list and press the unlock button.
    i think i have a problem to add a filepath from the listbox, because i get an error ( with try catch )

    Lock button:

    Code Block

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            status = arr(0)
            If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)
                Dim selectedpath As String = d.Parent.FullName + d.Name
                If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

                    If (Not d.Root.Equals(d.Parent.FullName)) Then
                        d.MoveTo(d.Parent.FullName & "\" & d.Name & status)
        
                    End If

                    Me.ListBox1.Items.Add(FolderBrowserDialog1.SelectedPath)
          
                End If

            End If

        End Sub


    unlock button ( with problem )
    Code Block

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Try
                status = arr(0)
                Dim d As DirectoryInfo = New DirectoryInfo(ListBox1.SelectedItem.ToString)
                Dim selectedpath As String = d.Parent.FullName + d.Name
                status = getstatus(status)
                d.MoveTo(FolderBrowserDialog1.SelectedPath(ListBox1.SelectedItem.ToString)) 
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            Catch ee As Exception
                MsgBox("Error")
            End Try
        End Sub


    i will berealy happy with some more help

    Monday, November 26, 2007 7:52 PM
  • Hi mNero,

     

    The code you want is as below:

    Prerequisites: FolderBrowserDialog1, ListBox1, LockButton and UnLockButton on Form1.

    Code Block

    Imports System.IO

    Public Class Form1

        Public status As String

        Private arr As String() = New String(5) {}

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            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}"

        End Sub

      

        ' Lock

        Private Sub LockButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LockButton.Click

            status = arr(0)

            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then

                Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

                Dim selectedpath As String = d.Parent.FullName + d.Name

                If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

                    If (Not d.Root.Equals(d.Parent.FullName)) Then

                        Dim newPath As String = d.Parent.FullName & d.Name & status

                        d.MoveTo(newPath)

                        Me.ListBox1.Items.Add(newPath)

                    End If

                End If

            End If

        End Sub

     

        ' UnLock

        Private Sub UnLockButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnLockButton.Click

            Try

                status = arr(0)

                Dim d As DirectoryInfo = New DirectoryInfo(ListBox1.SelectedItem.ToString)

                Dim selectedpath As String = d.Parent.FullName + d.Name

                Dim lockPath As String = ListBox1.SelectedItem.ToString

                If lockPath.LastIndexOf(".{") <> -1 Then

                    status = getstatus(status)

                    d.MoveTo(lockPath.Substring(0, lockPath.LastIndexOf(".")))

                    ListBox1.Items.Remove(ListBox1.SelectedItem)

                End If

            Catch ee As Exception

                MsgBox(ee.Message)

            End Try

        End Sub

       

        Private Function getstatus(ByVal stat As String) As String

            For i As Integer = 0 To 5

                If stat.LastIndexOf(arr(i)) <> -1 Then

                    stat = stat.Substring(stat.LastIndexOf("."))

                End If

            Next i

            Return stat

        End Function

     

    End Class

     

    Tuesday, November 27, 2007 3:23 AM
  • hmm....the lockButton isnt working well. it makes my folders disappear. and the unlock button doenst make them show
    when i make another folder and try to lock it, an error shows: cannot create a file when that file already exists ( my first folder with the same name has disappeared )

    i found the missing folders ( locked and unlocked )on disk C:
    Tuesday, November 27, 2007 8:37 AM
  • i have solved the probllem.

    for the lock button:
    it must be:

     Dim newPath As String = d.Parent.FullName & "\" & d.Name & status

    not:

    d.Parent.FullName & d.Name & status

    works now


    Tuesday, November 27, 2007 11:13 AM
  • I coded this awhile back and have been using it on Vista OS, which works fine. I then loaded the program onto my thumb drive to use it on multiple computer to lock folders. It appears to work fine except it doesn't seem to actually lock folder on XP OS. It shows the lock next to any folder but you can still access the files in the locked folder.

     

    Could there be something i coded incorectly? Or is this OS sensitive, only usably on VISTA?

     

    Here is my project code..........

     

    Form1:

     

    Imports System.IO

    Imports System.Drawing

    Imports System.Windows.Forms

     

     

    Public Class Form1

    Public status As String

    Private arr As String() = New String(5) {}

     

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    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}"

    End Sub

     

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim f As Password = New Password()

    If f.ShowDialog() = Windows.Forms.DialogResult.OK Then

    status = arr(0)

    If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

    Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

    Dim selectedpath As String = d.Parent.FullName + d.Name

    If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

    If (Not d.Root.Equals(d.Parent.FullName)) Then

    d.MoveTo(d.Parent.FullName & "\" & d.Name & status)

    Else

    d.MoveTo(d.Parent.FullName + d.Name & status)

    End If

    TextBox1.Text = FolderBrowserDialog1.SelectedPath

    ' PictureBox1.Image = Image.FromFile(Application.StartupPath & "\lock.jpg")

    Else

    status = getstatus(status)

    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")

    End If

    End If

    End If

    End Sub

     

     

    Private Function getstatus(ByVal stat As String) As String

    For i As Integer = 0 To 5

    If stat.LastIndexOf(arr(i)) <> -1 Then

    stat = stat.Substring(stat.LastIndexOf("."))

    End If

    Next i

    Return stat

    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    Dim f As Password = New Password()

    If f.ShowDialog() = Windows.Forms.DialogResult.OK Then

    status = arr(0)

    If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

    Dim d As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)

    Dim selectedpath As String = d.Parent.FullName + d.Name

    If FolderBrowserDialog1.SelectedPath.LastIndexOf(".{") = -1 Then

    If (Not d.Root.Equals(d.Parent.FullName)) Then

    d.MoveTo(d.Parent.FullName & "\" & d.Name & status)

    Else

    d.MoveTo(d.Parent.FullName + d.Name & status)

    End If

    TextBox1.Text = FolderBrowserDialog1.SelectedPath

    ' PictureBox1.Image = Image.FromFile(Application.StartupPath & "\lock.jpg")

    Else

    status = getstatus(status)

    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")

    End If

    End If

    End If

    End Sub

    End Class

     

    Form2 (password dialog box):

     

    Imports System.Text

    Partial Public Class Password

    Inherits Form

     

     

    Public Sub New()

    InitializeComponent()

    Me.Password = "123123"

    End Sub

     

     

    Private _password As String

     

     

    Public Property Password() As String

    Get

    Return Me._password

    End Get

    Set(ByVal Value As String)

    Me._password = getMd5Hash(Value)

    End Set

    End Property

    Shared Function getMd5Hash(ByVal input As String) As String

    ' Create a new instance of the MD5CryptoServiceProvider object.

    Dim md5Hasher As Security.Cryptography.MD5 = Security.Cryptography.MD5.Create()

    ' Convert the input string to a byte array and compute the hash.

    Dim data() As Byte = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

    ' Create a new Stringbuilder to collect the bytes

    ' and create a string.

    Dim sBuilder As StringBuilder = New StringBuilder()

    ' Loop through each byte of the hashed data

    ' and format each one as a hexadecimal string.

    Dim i As Integer

    For i = 0 To data.Length - 1 Step i + 1

    sBuilder.Append(data(i).ToString("x2"))

    Next

    ' Return the hexadecimal string.

    Return sBuilder.ToString()

    End Function

     

     

    ' Verify a hash against a string.

    Shared Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean

    ' Hash the input.

    Dim hashOfInput As String = getMd5Hash(input)

    ' Create a StringComparer an comare the hashes.

    Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

    If 0 = comparer.Compare(hashOfInput, hash) Then

    Return True

    Else

    Return False

    End If

    End Function

     

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim passwd As String = Me.TextBox1.Text

    If True = verifyMd5Hash(passwd, Me.Password) Then

    Me.DialogResult = Windows.Forms.DialogResult.OK

    Else

    MsgBox("Invalid Password", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)

    End If

    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub

    Private Sub Password_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    End Class

    Wednesday, March 5, 2008 10:54 PM