Install service and add registry entry under Vista+
I have a program that runs as a desktop application, but can also be installed as a windows service. The installation is done by using an "install as service" button on the GUI. The event handler for this button looks like this:
ProcessStartInfo psi = new ProcessStartInfo("sc", "description " +
this.ServiceName + " \"" +
((AssemblyDescriptionAttribute)attributes[0]).Description + "\"")
psi.CreateNoWindow = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.Verb = "runas";
Process.Start(psi).WaitForExit();
What I would like to do now is to add some additional registry entries under say:
HKLM\SOFTWARE\MyCompany\Services
which also needs elevated privileges. But if I use the same procedure as described above with the command开发者_StackOverflow "REG ADD" the UAC dialog would appear more than once - and I don't want that.
So whats the best way to install a windows service and add a registry entry under HKLM with only having the UAC prompt once?
Create a small exe or a batch file that does both things (the sc and the reg update). Launch that instead of sc.
As for installing service.
You can try topshelf. It is easy to use and very powerful.
I ended up calling myself with admin priviliges and a special parameter myprogram.exe -i
and then branch into a special method which uses the ServiceInstaller
class to install the program as a service (instead of calling sc).
精彩评论