9 Replies - 3777 Views - Last Post: 06 September 2011 - 09:18 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 10:37 AM

So I am able to create, keys, delete keys, and do everything else I want to do with the registry using C# thus far, but what if I just want to add a string to the RUN of the registry so a program will run on startup?

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", string.Empty, "C:\\Users\\Public\\hist.exe", RegistryValueKind.String);



The code above I would think should work. What am I missing? string.Empty, using the default value etc. Also I run this program with root/administrator privileges since you need to be admin to set value to this section of the registry, but the program still won't work. All Help Appreciated. You guys are the best :).

Is This A Good Question/Topic? 0
  • +

Replies To: Can't Set Value To Registry Key Using C# ?

#2 tlhIn`toq  Icon User is online

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

Reputation: 5316
  • View blog
  • Posts: 11,367
  • Joined: 02-June 10

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 10:49 AM

Is there a reason you don't want to put a shortcut in the user's Startup directory where the user can see it and know about it?

C:\Users\John Smith\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

This hints towards HIDING the fact that you are starting an application on launch, probably for malicious purposes.
Was This Post Helpful? 0
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 03:04 PM

I don't appreciate the accusation that I'm trying to write malware or be malicious. I have seriously way better things to do with my time. I don't like using the folder method as I feel it isn't as explicit.

Also the application needs administrator privileges to add a value to the Run registry key anyway, in which case someone could do a lot worse things than add a value to the run key. Your comment I'm sorry to say is so ridiculous on so many levels that it's really beyond words dude. If you don't have anything helpful to say....I'm sure you can finish the rest of the statment.

I'm doing this because on my application which will automatically delete web history (see my other posts) will have an install service button and run at startup. I like the registry route because it's explicit and I feel this is something I am going to need to know how to do for future applications to store values in the registry for user settings application settings etc.

This post has been edited by tlhIn`toq: 06 September 2011 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,393
  • Joined: 04-October 09

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 03:14 PM

View Postadn258, on 05 September 2011 - 03:04 PM, said:

I'm doing this because on my application which will automatically delete web history (see my other posts) will have an install service button and run at startup.

If it's a service, install it as a service, don't mess around with the registry.
Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 03:44 PM

I just used the HKEY_CURRENT_USER which is fine and should work fine for my application. Phew! Now that were done with that what do you mean install at it as a service? Use a windows service? I don't want to do that. What advantage for this little application would that pose anyway?


I'm not the only person using the registry for their application to run via startup. Why does everyone here keep acting like this is some dramatically evil horrible thing? It's working just fine :). If it works it works.

This post has been edited by tlhIn`toq: 06 September 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 04:21 PM

cause ppl can abuse this and use it for their malware and its something that DIC policy is against. anyway a 15 min search in the registry will give you the result you are looking for. as a programmer you should really learn to make research it will always save you a lot of time since your probably not the first one with that problem
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,393
  • Joined: 04-October 09

Re: Can't Set Value To Registry Key Using C# ?

Posted 05 September 2011 - 10:00 PM

View Postadn258, on 05 September 2011 - 03:44 PM, said:

I'm not the only person using the registry for their application to run via startup. Why does everyone here keep acting like this is some dramatically evil horrible thing? It's working just fine :). If it works it works.


From Microsoft: You should not edit registry data that does not belong to your application unless it is absolutely necessary.

That's why.
Was This Post Helpful? 0
  • +
  • -

#8 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Can't Set Value To Registry Key Using C# ?

Posted 06 September 2011 - 12:07 AM

I'm sorry, I find that silly. I'm doing what's most convenient for me not Microsoft. Is Microsoft our Dad or something? This sounds like Microsoft's putting their horrific malware and stupid user problem on me, which I have nothing to do with. Most Windows users probably wouldn't even have a clue about the registry, services, etc. (nor do they even care to know about it).

How dare Microsoft make this my problem. It's just ridiculous since Microsoft is the company that designed this OS structure INCLUDING THE REGISTRY in the first place. It's like a watch maker criticizing his own watches or something. Do I not have a point here?

To the guy who said research I did research the problem but couldn't find an answer originally as it has to do with a access to Local machine which I don't need in my case. Current User is fine.

This post has been edited by tlhIn`toq: 06 September 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

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

Reputation: 5316
  • View blog
  • Posts: 11,367
  • Joined: 02-June 10

Re: Can't Set Value To Registry Key Using C# ?

Posted 06 September 2011 - 07:27 AM

Just chiming in with my two cents worth:

"It works so what's the problem?" is a cry I/we hear a lot. Just because something works doesn't mean it was built in the best or even right way. As a programmer I'm sure you've run across other coder's code that is just a bird's nest of illegible crap that somehow manages to get the job done. I've seen people copy/paste the same block of 500 lines into 20 different methods because they didn't realize or take the time to make a single method that can take a parameter. It works. But it sucks.

You can have your program start by modifying the registry. Sure. Go for it. Personally I use the registry a lot for persistent settings of my applications, even though the suggested way is Application.Settings or an XML file. But that is a case of still following the guideline of only editing registry settings that DO belong to my application.

If it is working for you then fine. Just be aware that at some point you are are likely to find a machine where your installer doesn't have rights to write in the registry, or where an anti-virus app is triggered by your action, or a registry restoration script undoes your work at the close-of-day shutdown.

Maybe none of those situations apply to you right now in your environment. Maybe this application is only being used on machines you set up so you have control over everything. When you are the god of your world you can do anything you like. But be aware that if you go to work for someone else, try to use this same technique for a contract-for-bid side-job, do something like this for a buddy's work computer etc. that it may not be acceptable.

In your current programming environment you can think it is silly and you can choose to not follow industry standards and guidelines. That's your choice. But lots of companies have to and/or choose to for reasons that may mystify you right now, but make sense when the larger scope of their reasons is explained to you by the IT department head.
Was This Post Helpful? 1
  • +
  • -

#10 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Can't Set Value To Registry Key Using C# ?

Posted 06 September 2011 - 09:18 AM

Alright thanks....This answer makes more sense. Sorry if I got worked up about the whole thing but this is a good answer.

This post has been edited by tlhIn`toq: 06 September 2011 - 10:24 AM
Reason for edit:: No need to quote the entire long message that is immediately above.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1