2014-03-26 161 views
0

我有一個惱人的問題。 我正在嘗試使用RegDeleteKey值API刪除註冊表值(無我的名字空間),但只做了一處修改。 我希望函數有一個絕對路徑作爲參數。 因此,像這樣:註冊表刪除值

RegDeleteKeyValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","myValue") 

大氣壓它看起來像這樣(這一件作品,但沒有絕對路徑作爲參數,如它應該是):

Private Enum RegHive 
     HKEY_CLASSES_ROOT = &H80000000 
     HKEY_CURRENT_USER = &H80000001 
     HKEY_LOCAL_MACHINE = &H80000002 
     HKEY_USERS = &H80000003 
     HKEY_CURRENT_CONFIG = &H80000005 
    End Enum 

RegDeleteKeyValue(Reghive.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run","Myvalue") 

我試了一下修改像這樣的函數,所以參數將只是絕對路徑和註冊表值。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    DeleteReyKeyValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "123") 
End Sub 

Public Shared Function DeleteReyKeyValue(ByVal Key As String, ByVal valueName As String) As Long 
    Dim Handle As IntPtr 
    Dim Hive As String = Split(Key, "\")(0) 
    Select Case Hive 
     Case "HKEY_CLASSES_ROOT" 
      Handle = CType(&H80000000, IntPtr) 
     Case "HKEY_CURRENT_USER" 
      Handle = CType(&H80000001, IntPtr) 
     Case "HKEY_LOCAL_MACHINE" 
      Handle = CType(&H80000002, IntPtr) 
     Case "HKEY_USERS" 
      Handle = CType(&H80000003, IntPtr) 
     Case "HKEY_CURRENT_CONFIG" 
      Handle = CType(&H80000005, IntPtr) 
    End Select 
    Key = Key.Replace(Hive, String.Empty) 
    Return RegDeleteKeyValue(Handle, Key, valueName) 
End Function 

<DllImport("advapi32.dll")> _ 
Private Shared Function RegDeleteKeyValue(ByVal handle As IntPtr, ByVal keyName As String, ByVal valueName As String) As Long 
End Function 

當使用這個,沒有任何反應,沒有錯誤,但註冊表值不會被刪除? ; O 這是爲什麼?

回答

1

的問題已經回答計算器

所以刪除你的價值,你應該使用這個代碼,

string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run"; 
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true)) 
{ 
    if (key == null) 
    { 
     // Key doesn't exist. Do whatever you want to handle 
     // this case 
    } 
    else 
    { 
     key.DeleteValue("123"); 
    } 
} 

這會刪除註冊表中的值。我不確定你爲什麼要添加這樣複雜的代碼。 下面是MSDN的鏈接:http://social.msdn.microsoft.com/Forums/vstudio/en-US/5b22e94c-37a9-4be5-ad55-3d9229220194/how-to-use-add-read-change-delete-registry-keys-with-vbnet?forum=vbgeneral

它有VB代碼,但它仍然是幾乎爲C#一樣的,你可以使用一個在線轉換器的代碼轉換,http://converter.telerik.com/

0

感謝您的快速回復。 在這種情況下,一個朋友幫助我,代碼現在正在工作。 我只是缺少一個+ 「\」 這現在工作:

Public Shared Function DeleteReyKeyValue(ByVal Key As String, ByVal valueName As String) As Long 
    Dim Handle As IntPtr 
    Dim Hive As String = Split(Key, "\")(0) 
    MsgBox("Hive:" & Hive) 
    Select Case Hive 
     Case "HKEY_CLASSES_ROOT" 
      Handle = CType(&H80000000, IntPtr) 
     Case "HKEY_CURRENT_USER" 
      Handle = CType(&H80000001, IntPtr) 
     Case "HKEY_LOCAL_MACHINE" 
      Handle = CType(&H80000002, IntPtr) 
     Case "HKEY_USERS" 
      Handle = CType(&H80000003, IntPtr) 
     Case "HKEY_CURRENT_CONFIG" 
      Handle = CType(&H80000005, IntPtr) 
    End Select 
    Key = Key.Replace(Hive + "\", String.Empty) 
    Return RegDeleteKeyValue(Handle, Key, valueName) 
End Function 
<DllImport("advapi32.dll")> _ 
Private Shared Function RegDeleteKeyValue(ByVal handle As IntPtr, ByVal keyName As String, ByVal valueName As String) As Long 
End Function 

學分:羅津