justin
justin

Reputation: 21

Edit Registry in Vb.net

I need to edit registry in vb.net (2010) i know how to edit it in a .reg file but not in visual basic 2010 if it helps this is the code

    Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system]
"dontdisplaylastusername"=dword:00000000
"legalnoticecaption"="                                             Justin Tech"
"legalnoticetext"="This computer system, including all related equipment, is the property of the Justint Tech and is solely for uses authorized by jUSITN tECH. You have no right to privacy on the system, and all information and activity 

on the system may be monitored.  Any unauthorized use of the system may result in disciplinary action, civil or criminal penalties."
"shutdownwithoutlogon"=dword:00000001
"undockwithoutlogon"=dword:00000001

Upvotes: 2

Views: 6096

Answers (2)

Kev
Kev

Reputation: 119806

The Microsoft.Win32.RegistryKey class will provide you with all the functionality you need to read, modify and delete registry keys and values.

For example:

using Microsoft.Win32;

...

RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
       @"SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system", true);

if(myKey == null)
{
  myKey = Registry.LocalMachine.CreateSubKey(
             @"SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system",
             RegistryKeyPermissionCheck.ReadWriteSubTree);
}

myKey.SetValue("dontdisplaylastusername", 0, RegistryValueKind.DWord);
myKey.SetValue("legalnoticecaption", "Justin Tech", RegistryValueKind.String);
myKey.SetValue("legalnoticetext", "This computer system...", 
                                               RegistryValueKind.String);
myKey.SetValue("shutdownwithoutlogon", 1, RegistryValueKind.DWord);
myKey.SetValue("undockwithoutlogon", 1, RegistryValueKind.DWord);

The subkey HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies \system will actually exist, I only show a test that you'd do if you were creating your own keys and values for completeness.

Upvotes: 5

IAmTimCorey
IAmTimCorey

Reputation: 16757

Just as an alternative to working with registry keys directly in VB.NET, you could execute a .reg file directly using the following code:

Process.Start("regedit.exe", "fileName.reg /s")

The /s is to run it silently. The only catch here is that you might run into the possibility of someone else changing the .reg file and thus compromising your security. However, if you put the .reg file in a central place and made it read-only, you could execute this against your machines. This would allow you to change the contents of the .reg file without changing code.

Upvotes: 3

Related Questions