[VB] Sharing some code - Add Take Ownership to the Windows Context Menu

Discussion in 'Mixed Languages' started by QuantumBug, Mar 11, 2013.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,534
    1,396
    60
    #1 QuantumBug, Mar 11, 2013
    Last edited by a moderator: Apr 20, 2017
    Here I have written a process to add and remove Take Ownership from the Windows right-click menu...

    Is up to you how you implement it. Also, make sure you import Microsoft.Win32.

    Code:
    Imports Microsoft.Win32
    Code:
    #Region "Add Take Ownership"
    
        Private Sub installtakeown()
    
            Dim rk1 As RegistryKey
            Dim rk2 As RegistryKey
            Dim rk3 As RegistryKey
            Dim rk4 As RegistryKey
    
            Try
                rk1 = Registry.ClassesRoot.OpenSubKey("\*\shell\takeown", True)
                If rk1 Is Nothing Then
                    Registry.SetValue("HKEY_CLASSES_ROOT\*\shell\takeown", "", "Take Ownership", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\*\shell\takeown", "NoWorkingDirectory", "", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\*\shell\takeown", "HasLUAShield", "", RegistryValueKind.String)
                Else
                    rk1.Close()
                End If
    
                rk2 = Registry.ClassesRoot.OpenSubKey("\*\shell\takeown\command", True)
                If rk2 Is Nothing Then
                    Registry.SetValue("HKEY_CLASSES_ROOT\*\shell\takeown\command", "", "cmd.exe /c takeown /f ""%1"" && icacls ""%1"" /grant administrators:F", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\*\shell\takeown\command", "IsolatedCommand", "cmd.exe /c takeown /f ""%1"" && icacls ""%1"" /grant administrators:F", RegistryValueKind.String)
                Else
                    rk2.Close()
                End If
    
                rk3 = Registry.ClassesRoot.OpenSubKey("\Directory\shell\takeown", True)
                If rk3 Is Nothing Then
                    Registry.SetValue("HKEY_CLASSES_ROOT\Directory\shell\takeown", "", "Take Ownership", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\Directory\shell\takeown", "NoWorkingDirectory", "", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\Directory\shell\takeown", "HasLUAShield", "", RegistryValueKind.String)
                Else
                    rk3.Close()
                End If
    
                rk4 = Registry.ClassesRoot.OpenSubKey("\Directory\shell\takeown\command", True)
                If rk4 Is Nothing Then
                    Registry.SetValue("HKEY_CLASSES_ROOT\Directory\shell\takeown\command", "", "cmd.exe /c takeown /f ""%1"" /r /d y && icacls ""%1"" /grant administrators:F /t", RegistryValueKind.String)
                    Registry.SetValue("HKEY_CLASSES_ROOT\Directory\shell\takeown\command", "IsolatedCommand", "cmd.exe /c takeown /f ""%1"" /r /d y && icacls ""%1"" /grant administrators:F /t", RegistryValueKind.String)
                Else
                    rk4.Close()
                End If
    
                If rk1 Is Nothing = False And _
                   rk2 Is Nothing = False And _
                   rk3 Is Nothing = False And _
                   rk4 Is Nothing = False Then
    
                    MessageBox.Show("Take Ownership already exists.", "Information", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                Else
    
                    MessageBox.Show("Take Ownership was added to the Windows context menu.", "Information", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                End If
    
            Catch ex As Exception
                MessageBox.Show("An error occured in installtakeown.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
    #End Region
    Code:
    #Region "Remove Take Ownership"
    
        Private Sub removetakeown()
    
            Dim rk1 As RegistryKey
            Dim rk2 As RegistryKey
    
            Try
                rk1 = Registry.ClassesRoot.OpenSubKey("\*\Shell\takeown", False)
                rk2 = Registry.ClassesRoot.OpenSubKey("\Directory\Shell\takeown", False)
    
                If rk1 Is Nothing And _
                    rk2 Is Nothing Then
    
                    MessageBox.Show("Take ownership is already disabled.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                Else
    
                    Registry.ClassesRoot.DeleteSubKeyTree("\*\Shell\takeown")
                    Registry.ClassesRoot.DeleteSubKeyTree("\Directory\Shell\takeown")
    
                    rk1.Close()
                    rk2.Close()
    
                    MessageBox.Show("Take ownership was removed from the Windows context menu.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                End If
            Catch ex As Exception
                MessageBox.Show("An error occured in removetakeown.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
    #End Region
    If anyone has any questions or needs any help with this code, please ask.

    Enjoy, The Dev.

    Edit #2: I've removed some unnecessary code. (again)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. HALIKUS

    HALIKUS MDL Addicted

    Jul 29, 2009
    527
    369
    30
    #2 HALIKUS, Jun 15, 2013
    Last edited by a moderator: Apr 20, 2017
    Code:
    Windows Registry Editor Version 5.00
    
    ;Add "Take Ownership" to context menu
    [HKEY_CLASSES_ROOT\Directory\shell\runas]
    @="Take Ownership"
    "NoWorkingDirectory"=""
    "icon"="UserAccountControlSettings.exe"
    
    [HKEY_CLASSES_ROOT\Directory\shell\runas\command]
    @="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
    "IsolatedCommand"="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
     
  3. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,534
    1,396
    60
    That is exactly what I converted into VB.NET. But thanks anyhow.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...