3 Replies - 3432 Views - Last Post: 03 June 2010 - 08:33 PM Rate Topic: -----

#1 Backtracker  Icon User is offline

  • D.I.C Head

Reputation: -23
  • View blog
  • Posts: 163
  • Joined: 30-November 08

Add A Value To A Registry Key

Posted 03 June 2010 - 01:10 PM

How can i add a value to this?
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
        Dim regKey As RegistryKey
        regKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion", True)
        regKey.CreateSubKey("Run")
        regKey.SetValue("RSL", "IT WORKS!!!")
        regKey.Close()

i wanna add this: regKey.SetValue("RSL", Application.StartupPath & "RSL.exe")

Is This A Good Question/Topic? 0
  • +

Replies To: Add A Value To A Registry Key

#2 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: Add A Value To A Registry Key

Posted 03 June 2010 - 01:20 PM

Are you getting an error?
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Add A Value To A Registry Key

Posted 03 June 2010 - 01:47 PM

You do realize you will need to make the string first for this, then add it. Right?

Quote

Application.StartupPath & "RSL.exe


I'm not good at VB, but I can show you an example in C#.

RegistryKey rKey, rKey1;
rKey = Registry.CurrentUser;
rKey.OpenSubKey(@"HKEY_CURRENT_USER\SOFTWARE", true);

rKey1 = rKey.CreateSubKey("Software\\" +
                            Application.CompanyName + "\\" +
                            Application.ProductName +
                            "\\MySettings\\");
rKey1.SetValue("PreferedColor", "Green");

As you can see, a second RegistryKey is used to become the referenced object from making the new subkey. Then you add the new value to that.

I'll take a stab at what you need for VB, but you will probably have to fix some errors

Dim regKey As RegistryKey
Dim regKey1 As RegistryKey
Dim FinalPath As String
FinalPath = Application.StartupPath & "RSL.exe"
regKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion", True)
regkey1 =  regKey.CreateSubKey("Run")
regKey1.SetValue("RSL", FinalPath)
regKey.Close()
regKey1.Close()

Was This Post Helpful? 2
  • +
  • -

#4 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 372
  • Joined: 17-July 09

Re: Add A Value To A Registry Key

Posted 03 June 2010 - 08:33 PM

 Dim regKey As RegistryKey
        regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
        regKey.SetValue("RSL", Application.StartupPath & "RSL.exe")
        regKey.Close()



Because the subkey "RUN" is most likely already present, you dont need to create it and so you can go straight to it.

But if the key already exists and you have a regKey.CreateSubKey("Run") it wont add the "Setvalue"

So, you will need a little bit more code to first check if the subkey exists and if so, bypass the createkey otherwise create it first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1