Modifica dei dati del Registro di sistema

La classe Del provider del Registro di sistemaStdRegProv per WMI include metodi che eseguono le operazioni seguenti:

  • Creare o eliminare chiavi del Registro di sistema.

    Usare CreateKey o DeleteKey.

  • Creare o eliminare valori denominati, denominati voci quando si trovano in chiavi.

    Usare il nome di un nuovo valore e SetBinaryValue, SetDWORDValue, SetExpandedStringValue, SetMultiStringValue o SetStringValue per creare un valore denominato. Usare DeleteValue per eliminare un valore denominato.

  • Modificare i valori denominati.

    Usare il nome di un valore e i metodi Set (identificati nell'elemento puntato precedente) per modificare i valori denominati esistenti in una chiave. È necessario conoscere il nome di un valore per modificarlo. Se non si conoscono i nomi dei valori in una chiave, utilizzare il metodo EnumValues per ottenere i nomi.

In questo argomento vengono illustrate le sezioni seguenti:

Creazione di una chiave del Registro di sistema con VBScript

Poiché il Registro di sistema è il database di configurazione centrale per il sistema operativo, le applicazioni e i servizi, prestare attenzione quando si scrivono modifiche ai valori del Registro di sistema o si eliminano le chiavi.

Nota

Non è possibile monitorare la sottochiave HKEY_CLASSES_ROOT di HKEY_CURRENT_USER(HKCU). Il monitoraggio HKEY_USERS non è consigliato perché le sottochiavi vengono visualizzate e scompaiono man mano che vengono caricate le hive.

 

Gli esempi di codice seguenti illustrano come creare una nuova chiave del Registro di sistema e una sottochiave.

HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set ObjRegistry = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strPath = "SOFTWARE\MyKey\MySubKey"

Return = objRegistry.CreateKey(HKEY_LOCAL_MACHINE, strPath)

If Return <> 0 Then
    WScript.Echo "The operation failed." & Err.Number
    WScript.Quit
Else
    wScript.Echo "New registry key created" & VBCRLF & "HKLM\SOFTWARE\MYKey\"

End If

$HKEY_LOCAL_MACHINE = 2147483650 
$strComputer = "."
$strPath = "SOFTWARE\MyKey\MySubKey"

$reg = [wmiclass]"\\$strComputer\root\default:StdRegprov"

[void]$reg.CreateKey($HKEY_LOCAL_MACHINE, $strPath)

Creazione di un valore del Registro di sistema denominato tramite PowerShell e VBScript

L'esempio di codice seguente illustra come creare un valore denominato denominato MultiStringValue nella chiave HKEY_LOCAL_MACHINE\SOFTWARE\MyKey\MySubKey creata dallo script precedente. Lo script chiama StdRegProv.SetMultiStringValue per scrivere valori stringa in un nuovo valore denominato.

const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "."

Set objRegistry = _
    GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\MyKey\MySubKey"
strValueName = "MultiStringValue"
arrStringValues = Array("one", "two","three", "four")

objRegistry.SetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath,_
    strValueName, arrStringValues

' Read the values that were just written
objRegistry.GetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath,_
    strValueName, arrStringValues   

For Each strValue in arrStringValues
    WScript.Echo strValue 
Next

$HKEY_LOCAL_MACHINE = 2147483650 
$strComputer = "."
$strPath = "SOFTWARE\MyKey\MySubKey"

$strValueName = "MultiStringValue"
$arrStringValues = @("one", "two","three", "four")

$reg = [wmiclass]"\\$strComputer\root\default:StdRegprov"

[void]$reg.SetMultiStringValue($HKEY_LOCAL_MACHINE, $strKeyPath, $strValueName, $arrStringValues)

$multiValues = $reg.GetMultiStringValue($HKEY_LOCAL_MACHINE, $strKeyPath, $strValueName)
$multiValues.sValue

Con WMI non è possibile impostare la sicurezza di accesso su una chiave del Registro di sistema. Tuttavia, il metodo StdRegProv.CheckAccess confronta le impostazioni di sicurezza dell'utente corrente con il descrittore di sicurezza in una chiave del Registro di sistema per determinare se l'utente dispone di un'autorizzazione specifica, ad esempio KEY_SET_VALUE.