2015-12-03 77 views
1

我已經完成了這段代碼,但它似乎並不奏效。我不知道爲什麼。我在谷歌搜索,但沒有運氣。 :(試圖在註冊表數據庫中創建一個密鑰

Set objREG = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
Const HKEY_LOCAL_MACHINE = &H80000002 
Dim lstrKeyPath, lstrValueName, lstrValue 
lstrKeyPath = "SOFTWARE\Canon\GARO1\" 
lstrValueName = "LocaleInfo" 
objReg.GetStringValue HKEY_LOCAL_MACHINE,lstrKeyPath,lstrValueName,lstrValue 
msgbox lstrValue <--- This works. 
if IsNull(lstrValue) then 
    lstrKeyPath = lstrKeyPath & lstrValueName 
else 
    lstrValueName = "LocaleTest" 
    lstrKeyPath = "Software\Test\" 
    Return = objReg.CreateKey(HKEY_LOCAL_MACHINE,lstrKeyPath) 
    if Return = 0 Then 
    msgbox "Yes" 
    else 
    msgbox "No" 
    end if 
end if 
Set OBJREG = Nothing 

我看不到我的「測試」鍵

回答

0

我已經使用了以下檢查,並插入註冊表項,如果它們不存在註冊表:

Dim WshShell, Test, blExists, DQ 
Set WshShell = CreateObject("WScript.Shell") 
DQ = chr(34) 

RegKeyPath = "HKEY_CLASSES_ROOT\LFS\" 
RegValueName = "URL:LFS Protocol" 
Test = RegKeyExists(RegKeyPath,RegValueName) 

If Test = False Then 
    WshShell.RegWrite RegKeyPath, "URL:LFS Protocol" ,"REG_SZ" 
End If 

'Function Returns False if the regkey isnt found otherwise it returns 
'The registry key value specified 
Function RegKeyExists(sRegKey,sRegValueName) 

    On Error Resume Next  

    Dim WSHShellRegTest, Test, blExists 
    Set WSHShellRegTest = CreateObject("WScript.Shell") 

    blTrueFalse = True 

    Test = WSHShellRegTest.RegRead (sregkey + sRegValueName) 

    If Err Then 
     RegKeyExists = False 
     Err.clear 
     Exit Function 
    End if 

    Set WSHShellRegTest = Nothing 

    RegKeyExists = Test 

End Function 
相關問題