Can't read key? - vb.net

Ok so I am making this system information application that runs on the desktop, but for some reason this gives me an error.
Error: [ArgumentException was unhandled] Registry key name must start with a valid base key name.
I can see the registry is there.
Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\3","ProcessorNameString",Nothing)
Label9.Text = CStr(readValue)
Edit: This sight just helped me, there was 2 Unicode chars in there.

I think the problem with program permission ,HKEY_LOCAL_MACHINE needs administrator access, in app.manifest
Change
to

Related

Save and Get a Registry Value in 'HKEY_LOCAL_MACHINE\Software' using VB.NET

In my VB.NET project I want to save and get a registry value in "HKEY_LOCAL_MACHINE\Software" but I am only able to save and get it in "HKEY_LOCAL_MACHINE" but not software.
Here is my code:
For setting the value
My.Computer.Registry.LocalMachine.SetValue("Study", "1")
For getting the value
Dim RegistryCheck As String = My.Computer.Registry.LocalMachine.GetValue("Study")
You have to verify if you have the permission to write (and also read) from HKEY_LOCAL_MACHINE. If you can't get the required permissions (which is quite possible), opt for HKEY_CURRENT_USER (My.Computer.Registry.CurrentUser). Anyway, you have to specify what kind of permissions you're requiring: Read/ReadWrite. See the overloads of the OpenSubKey() method. Two of them let's you specify these requirements. If/When you are granted the permission, create the subkey (CreateSubKey()) and then set a key value inside it (with SetValue()), specifying a value type with RegistryValueKind.

How to find the path to the executable of a program

In a VB.NET application I need to set a button to start a program - specifically, Steam. But I need to allow for the user having installed the program somewhere other than the default location "C:\Program Files (x86)\Steam". So all I know is the executable name: "steam.exe".
In searching for an answer it looks like I should be able to get the path using Microsoft.Win32.Registry somehow, but the examples I'm seeing aren't helping me (I guess I'm just not getting how to apply those examples to my situation).
This tutorial gives the example of finding the path to the excel executable:
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MICROSOFT\Windows\CurrentVersion\AppPaths\excel.exe")
Dim path As String = regKey.GetValue("Path").ToString
But I'm getting a value of "Nothing" for regKey. Not only that, even if it did get a value for regKey, how would I know the registry path for another program (Steam, in my case)?
And how much will the version of Windows change the possible registry location of a program - if at all?

Registry key error if the folder doesn't exist in VB

I am trying to add a registry key in to the file "MyApp" which doesn't yet exist in the registry key directory, when I try and write this key however I get an error from the debug console telling me the "NullReference was unhanded". If I go and manually make this folder it works brilliantly, so can anyone help me as I thought that this code would make the folder as well? If you could show me what code needs to also be there I would be very grateful!
The code I am using is:
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True).SetValue("AppName", "SerialKey")
OpenSubKey("SOFTWARE\MyApp", True) will open the key for write access only if it already exists. If the key doesn't exist, it will return null. Since you are blindly calling SetValue on a null reference, you will get the null reference exception.
The simplest solution to your requirement is to call CreateSubKey("SOFTWARE\MyApp") instead. This does exactly what you want, i.e. creates a new subkey or opens the existing subkey for write access.
adding and editing LocalMachine registry key needs administrator privilege in win 7, may be you are getting the error because of this

VB.NET read registry

Beyond perplexed this time...
The simplest possible line of code works sometimes, sometimes it doesn't. First I thought the issue was that I was trying to read the value of a DWORD, but since I CAN read DWORD values from SOME keys, that must not be the problem. Now the problem seems to be that I can't read from ANY key if the key has a space in the name. Surely this can't be. I refuse to believe that MS didn't account for spaces in registry key paths and names.
So tell me why this doesn't work:
MsgBox(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\CA\CA ARCserve D2D\WebService", "Port", Nothing))
It just pops up an empty box. And yes, a value does exist in the registry, and yes, I have permission to read the key.
EDIT: Yup, over and over again it seems that you can't read from the registry if there are spaces anywhere in the key name. Seriously?!?
EDIT AGAIN: "Ramhound" says code examples are stupid. Fascinating point of view. However his own suggestion also failed:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\ATI Technologies\CBT")
Dim objValue As Object
objValue = key.GetValue("ReleaseVersion")
MsgBox(objValue.ToString())
After an entire wasted day, the solution is to set your VS project to "any cpu" in advanced compile options because if set to x86 and running on a 64bit OS you are limited to the "Wow6432node" in the registry.
It's also worth noting than on a 64 bit version of Windows 7 while running a vb.net app in 32 bit mode, the Wow6432Node key is hidden from you when using a Microsoft.Win32.RegistryKey object. I'd written this code to check which key I needed to read to get the right ODBC Driver subkey:
Dim myReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim myReg_Key As Microsoft.Win32.RegistryKey
myReg_Key = myReg.OpenSubKey("SOFTWARE")
strRegistry_Keys = myReg_Key.GetSubKeyNames()
bool64_Bit_OS = False
For Each strSub_Key As String In strRegistry_Keys
If strSub_Key = "Wow6432Node" Then
bool64_Bit_OS = True
End If
Next
When you do a GetSubKeyNames() on the "SOFTWARE" key you are redirected to SOFTWARE\Wow6432Node
This does make it easier for my code as now I don't need to work out which subkey to look in to find which Oracle ODBC driver to use.
Kristian

"The directory name is invalid" error on Process.Start?

I am writing a launcher program, and when I go to start the process I get the "The directory name is invalid" error. Here is the code that is launching the process:
Const DEBUG_ROOT = _
"Z:\Kiosk_JC\KioskSignIn.root\KioskSignIn\KioskSignIn\KioskSignIn\bin\Debug"
Dim oKiosk As New System.Diagnostics.Process
oKiosk.StartInfo.UserName = oEnc.Decrypt(Username)
oKiosk.StartInfo.Password = oEnc.DecryptSecure(Password)
oKiosk.StartInfo.Domain = oEnc.Decrypt(Domain)
''// The AddBS function appends a '\' to the passed string if it is not present
oKiosk.StartInfo.WorkingDirectory = AddBS(DEBUG_ROOT)
oKiosk.StartInfo.FileName = "KioskSignIn.exe"
oKiosk.StartInfo.UseShellExecute = False
Dim proc As Process = Nothing
proc = System.Diagnostics.Process.Start(oKiosk.StartInfo)
I saw on another question here that I needed to set the WorkingDirectory (before I started searching I was getting the error). Even though I have this property set, I am still getting the error. Any thoughts?
More info
I should also note that my Z:\ is a on my network. I have a function that resolves a path to UNC. When I ran this function on DEBUG_ROOT, I get the same error.
I tried moving the application to c:\kiosk. Same result. I am logged in as the user I am impersonating, so I have access to all shares and files.
Here is the link, for some reason the URL formating wants to consume all the text after the link is designated:
Referred Post
Mapped drives are per-user. You are likely starting the process with a different user.
Sounds like the process can't see the Z: drive or doesn't have security access. What user context does the app run under? Perhaps the Z: drive is not available in that context.
I got the same error as you do. most likely the user you use to run the process does not have access to specified resource (exe file)
try to move your exe to some other location and/or give your user access rights to the file.