给windows服务添加描述
2024-07-21 02:16:05
供稿:网友
最近写了个windows服务(windows services),安装了以后,觉得和已有的windows服务不一样。为什么?我的缺少描述,中间一栏是空的。
再看.net的servicebase类没有添加描述的属性。
public class projectinstaller : system.configuration.install.installer中也没有什么属性来添加。从网搜了后才知道要重载projectinstaller 的install和uninstall虚方法。其实重写这些虚方法就是为了在注册表相应服务中加一个鍵值"description",其值填为相应的描述就可以了。
public override void install(idictionary stateserver)
{
microsoft.win32.registrykey system,
service,
config;
try
{
//let the project installer do its job
base.install(stateserver);
system = microsoft.win32.registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("services");
service = system.opensubkey(this.serviceinstaller1.servicename, true);
service.setvalue("description", "服务描述");
//添加额外的鍵
config = service.createsubkey("additionalinformation");
}
catch(exception e)
{
}
}
public override void uninstall(idictionary stateserver)
{
microsoft.win32.registrykey system,
currentcontrolset,
services,
service;
try
{
system = microsoft.win32.registry.localmachine.opensubkey("system");
currentcontrolset = system.opensubkey("currentcontrolset");
services = currentcontrolset.opensubkey("services");
service = services.opensubkey(this.serviceinstaller1.servicename, true);
//删除额外的鍵
service.deletesubkeytree("additionalinformation");
//...
}
catch(exception e)
{
}
finally
{
base.uninstall(stateserver);
}
}
注意这些代码是在projectinstaller 文件中的。
也许有更好的办法,望大家指教。