Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

You can spawn the regedit.exe command utility to simply mimic it's behavior in your C# application, here is how...

One of my clients needed to run a script on a user machine every time the user logged in. The script would import some registry keys and settings that customize certain parts in Windows for their client.

My task was to write the script. Well, my first thought was to write a text parser for their .reg file, then using the .NET Registry classes to import all the keys into the registry one by one.

That seemed like a lot of work for a lazy C# slacker like me :-) so I decided to see if I can reuse some tested code out there, and it hit me... what's better than Microsoft's own regedit.exe utility!

regedit.exe is not just a GUI utility, It can also be used from the command line with the /s option. So if I can spawn the utility and pass it my reg file, I'm pretty much done!

So here is what I've done:

using System.Diagnostics;

static void Main ( string [] args)

{

    Process regeditProcess = Process .Start( "regedit.exe" , "/s "  

       +args[0].ToString());

    regeditProcess.WaitForExit(regeditTimeOut);

}

 

And that's the whole script! You'd pass the .reg file name you want to import to the registry to the program command  arguments, and it passes it back to regedit.exe, and you are done! Note the call to WaitForExit, this allows your script to wait until regedit.exe process is finished importing the file.

It's good to be lazy sometimes!

kick it on DotNetKicks.com

Feedback

Posted on 4/1/2010 12:36:22 PM

Nice. I was thinking about writing a parser as well, but this will do the trick.

Note: Either the Process.Dispose() method should be called or wrap the calls with a using statement like this:

const int regeditTimeOut = 10000; //10 second timeout
using (Process regeditProcess = Process.Start("regedit.exe","/s " + args[0].ToString()))
{
regeditProcess.WaitForExit(regeditTimeOut);
}

Posted on 11/17/2009 9:47:09 AM

Much appreciated- this is exactly what I as looking for!

Posted on 5/23/2009 11:49:04 AM

This is a great solution which I also use for importing. Just for reference, you can also use a /E and the filename to export the registry as well - great for backup & restore

Posted on 3/23/2009 6:41:13 AM

Hi,
a cool article... good one!! I need to export certain key values to a config file how can i do that in C#? Thanks in advance.

Posted on 11/5/2008 3:15:05 AM

Awesome...got exactly what was needed. Thanks to the author and Google that got me to this page.

Posted on 5/26/2008 9:29:24 AM

Great Thanks for this article. Now I'm writing course project and it's very usefull for me. Thanks.

Posted on 7/24/2007 2:42:32 AM

Thanks! Just what I needed, and it took me 5 secs to find it too.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author