The content of this article or any related information is under the Creative Commons license BY, you can republish this content freely but you must mention the author of this article: Kernel and indicate the URL of this page: https://www.exabyteinformatica.com/tienda/foro/escribir-un-valor-en-el-registro-de-windows-desde-vb-t1074.html
Si queremos crear una entrada en el registro de Windows desde Visual Basic, podemos recurrir al siguiente código:
Código: Seleccionar todo
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa",
"LmCompatibilityLevel", "1", Microsoft.Win32.RegistryValueKind.DWord)
El ejemplo anterior escribiría un valor DWORD ("1") en la ruta especificada y creando la entrada LmCompatibilityLevel. Los diferentes tipos de variables que podemos crear son:
REG_SZ: Una cadena de texto de longitud determinada. Valores booleanos (True o False) y otro tipo de texto corto son los valores que se usan en éste tipo de datos.
REG_EXPAND_SZ: Una variable de texto que puede incluir variables que son resueltas cuando la aplicación o el servicio use estos datos.
REG_DWORD: Datos representados por un número que es de 4 bytes (32 bits) de longitud.
REG_MULTI_SZ: Múltiples matrices de texto formateados y terminados con dos caracteres nulos.
Microsoft.Win32.RegistryValueKind.DWord => crea una clave DWORD pero podemos crear diferentes tipos de valores, desde Visual Basic podemos consultar las otras entradas desde el código fuente.
Si queremos comprobar si antes el valor ya existe usaremos:
Código: Seleccionar todo
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa",
"LmCompatibilityLevel", Nothing) Is Nothing Then
' <código_que_queramos_usar>
EndIf
Finalmente si queremos borrar un valor del registro de Windows podemos usar este código:
Código: Seleccionar todo
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software", True)
regKey.DeleteSubKey("MyApp", True)
regKey.Close()
No te pierdas el tema anterior: Pregunta de filtros a un datagrid...
Salta al siguiente tema: Ejecutar dobles comillas en Shell() en Visual Basic
Quizás también te interese: