我正在尝试运行一个作为窗口服务的exe文件。我以前做过这样的手工操作:
sc create TestService binPath= "C:\MyExePathWhichIsToBeRunAsWindowService" 而且它可以正常工作,当我看到我能够找到的服务时,现在必须使用c#代码进行同样的操作。
代码应该向用户询问exe文件的路径,这个文件必须作为窗口服务运行,以及他必须提供给这个窗口service.So的名称--这两件事情用户将在运行时输入,这对我来说是一项简单的任务,但一旦我得到它,我将如何从c#代码运行下面的命令?
sc create TestServiceNameUsrEntered binPath= "path user entered for exe at run time" 有人能帮帮我吗?
编辑:请注意,用户将始终输入serviceApplication exe文件而不是任意文件
发布于 2015-10-29 13:19:58
你应该调查一下Process.Start。你可能想试试这样的方法:
Process.Start("sc", String.Format("create \"{0}\" binPath=\"{1}\"", serviceName, binPath));发布于 2015-10-29 12:58:36
你可以看看顶架。
如果您喜欢自己从头开始操作,可以查看HostInstaller.cs,它只需添加所需的注册表项:
using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
using (RegistryKey service = services.OpenSubKey(_settings.ServiceName, true))
{
service.SetValue("Description", _settings.Description);
var imagePath = (string)service.GetValue("ImagePath");
_log.DebugFormat("Service path: {0}", imagePath);
imagePath += _arguments;
_log.DebugFormat("Image path: {0}", imagePath);
service.SetValue("ImagePath", imagePath);
}https://stackoverflow.com/questions/33414583
复制相似问题