Writing reg_multi_sz values in VB.NET

S

Steve Bernard

Hey there, I was wondering if anyone has been able to write
reg_multi_sz multi-string values to the registry from VB.NET. All the
registry stuff I find from MS can't differentiate different types of
strings.

I see some examples of VBScript that use the WMI registry provider to
write to the registry. Would this be possible to adapt to VB.NET.
I'm looking at this script right now:
http://cwashington.netreach.net/depo/view.asp?Index=560&ScriptType=vbscript

I'm a bit new to programming in general, though I do know how to set
this value using some other tools, like WinBatch. If anyone has a
good method of setting this via .NET, please let me know, as I'd
really appreciate it.

Thanks,

-Steve
 
S

Steve Bernard

I've made a bit of progress here. I have a simple script I can run
which looks like this:

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\CMA\Test"
MultValueName = "BootExecute"
strComputer = "."
iValues = Array("Autocheck autochk /p \??\C:", "Autocheck autochk
/p \??\D:")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
strComputer & "\root\default:StdRegProv")
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,
MultValueName, iValues

This works fine, and writes the value I want to a testing area in my
registry.

I've tried to move this over to VB.NET and I have this:

Const HKEY_LOCAL_MACHINE = &H80000002
Dim strKeyPath As String
Dim MultValueName As String
Dim strComputer As String
Dim oReg As Object
Dim iValues(1) As String
strKeyPath = "SOFTWARE\CMA\Test"
MultValueName = "BootExecute"
strComputer = "."
iValues(0) = "Autocheck autochk /p \??\C:"
iValues(1) = "Autocheck autochk /p \??\D:"
oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
& strComputer & "\root\default:StdRegProv")
oReg.CreateKey(HKEY_LOCAL_MACHINE, strKeyPath)
oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE, strKeyPath,
MultValueName, iValues)

I'm referencing and importing System.Management, and the code *almost*
works. In fact, the only place it's having trouble is in the last
line, specifically where I'm putting in "iValues". If I leave off the
whole last line, my program will correctly create the key. If I have
the last line as `oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE,
strKeyPath, MultValueName)' (that is, without the iValues in there), I
wind up with a REG_MULTI_SZ value in the right place with the value of
"hello there", which I guess is some sort of default value that gets
shoved in there if I don't specify anything.

So, that's where I'm at with my current code. I can create a key, and
I can create a multi-string value, but I can't set what that value is,
which makes it Not Too Useful. If anyone can see the obvious flaw in
what I'm doing on that last line (or in how I set up the string array
in the first place), please post.

-Steve
 
S

Stephen Martin

Try this:

Dim rk As Microsoft.Win32.RegistryKey =
Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\A Multitest")
rk.SetValue("Stuff", New String() {"This", "is", "an", "array", "of",
"strings"})
rk.Close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top