Drive Locker in Visual Basic.Net
Posted by Samath
Last Updated: December 31, 2016

This program will lock any hard drive or removable drive on the PC.

Imports Microsoft.Win32
Imports System.IO
Public Class Form1
    Dim regkey As RegistryKey
    Dim Regpath As String = "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Dim OsDrive As String
    Dim DriveDword, Loc As Int32

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonLock.Click
        OsDrive = Path.GetPathRoot(Environment.SystemDirectory)
        If comboBox1.Text.Length > 0 Then
            If Not OsDrive = comboBox1.Text Then
                DriveDword = Driv(comboBox1.Text)
                regkey = Registry.CurrentUser.OpenSubKey(Regpath, True)
                regkey.SetValue("NoViewOnDrive", DriveDword, RegistryValueKind.DWord)
                MessageBox.Show("Drive Locked was successful")
                For Each myp In Process.GetProcessesByName("explorer")
                    myp.Kill()
                Next
                Shell("explorer.exe", AppWinStyle.Hide)
            Else
                MessageBox.Show("You cannot Lock this drive because it contains your operating system", "Error")
            End If
        Else
            MessageBox.Show("Please select a drive to Lock")
        End If

    End Sub

    Function Driv(ByVal Drive As String) As Int32
        If Drive = "A:\" Then
            Loc = 0
        ElseIf (Drive = "B:\") Then
            Loc = 2
        ElseIf (Drive = "C:\") Then
            Loc = 4
        ElseIf (Drive = "D:\") Then
            Loc = 8
        ElseIf (Drive = "E:\") Then
            Loc = 16
        ElseIf (Drive = "F:\") Then
            Loc = 32
        ElseIf (Drive = "G:\") Then
            Loc = 64
        ElseIf (Drive = "H:\") Then
            Loc = 128
        ElseIf (Drive = "I:\") Then
            Loc = 256
        ElseIf (Drive = "J:\") Then
            Loc = 512
        ElseIf (Drive = "K:\") Then
            Loc = 1024
        ElseIf (Drive = "L:\") Then
            Loc = 2048
        ElseIf (Drive = "M:\") Then
            Loc = 4096
        ElseIf (Drive = "N:\") Then
            Loc = 8192
        ElseIf (Drive = "O:\") Then
            Loc = 16384
        ElseIf (Drive = "P:\") Then
            Loc = 16384 * 2
        ElseIf (Drive = "Q:\") Then
            Loc = 65536
        ElseIf (Drive = "R:\") Then
            Loc = 131072
        ElseIf (Drive = "S:\") Then
            Loc = 262144
        ElseIf (Drive = "T:\") Then
            Loc = 262144 * 2
        ElseIf (Drive = "U:\") Then
            Loc = 1048576
        ElseIf (Drive = "V:\") Then
            Loc = 1048576 * 2
        ElseIf (Drive = "W:\") Then
            Loc = 1048576 * 4
        ElseIf (Drive = "X:\") Then
            Loc = 1048576 * 8
        ElseIf (Drive = "Y:\") Then
            Loc = 1048576 * 16
        ElseIf (Drive = "Z:\") Then
            Loc = 1048576 * 32
        End If
        Return Loc
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.User.IsInRole("administrators") = True Then
            For Each drive In Environment.GetLogicalDrives
                Dim InfoDrive As DriveInfo = New DriveInfo(drive)
                If InfoDrive.DriveType = DriveType.Removable Or InfoDrive.DriveType = DriveType.Fixed Then
                    comboBox1.Items.Add(drive)
                End If
            Next
            comboBox1.SelectedIndex = 0
            comboBox1.Text = comboBox1.SelectedItem.ToString
        Else
            MessageBox.Show("Administrators access rights are required.", "Not Administrator")
            End
        End If
    End Sub

    Private Sub buttonUnlock_Click(sender As Object, e As EventArgs) Handles buttonUnlock.Click
        regkey = Registry.CurrentUser.OpenSubKey(Regpath, True)
        If Not regkey.ValueCount = 0 Then
            regkey.DeleteValue("NoViewOnDrive")
            MessageBox.Show("Drive UnLocked was successful")
            For Each myp In Process.GetProcessesByName("explorer")
                myp.Kill()
            Next
            Shell("explorer.exe", AppWinStyle.Hide)
        Else
            MessageBox.Show("There is No Locked Drive")
        End If
    End Sub

    Private Sub comboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comboBox1.KeyPress
        e.Handled = True
    End Sub
End Class