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!