C# 如何使用c作为窗口服务运行exe#
我正在尝试将exe文件作为窗口服务运行。我以前曾通过以下方式手动完成此操作:C# 如何使用c作为窗口服务运行exe#,c#,.net,service,windows-services,servicecontroller,C#,.net,Service,Windows Services,Servicecontroller,我正在尝试将exe文件作为窗口服务运行。我以前曾通过以下方式手动完成此操作: sc create TestService binPath= "C:\MyExePathWhichIsToBeRunAsWindowService" Process.Start("sc", String.Format("create \"{0}\" binPath=\"{1}\"", serviceName, binPath)); 当我看到我能够找到的服务时,它可以正常工作,现在我必须使用c代码来做同样的事情 代
sc create TestService binPath= "C:\MyExePathWhichIsToBeRunAsWindowService"
Process.Start("sc", String.Format("create \"{0}\" binPath=\"{1}\"", serviceName, binPath));
当我看到我能够找到的服务时,它可以正常工作,现在我必须使用c代码来做同样的事情
代码应该询问用户exe文件的路径,该文件必须作为窗口服务运行,并且还必须提供给该窗口服务的名称。因此,用户将在运行时输入这两件事,这对我来说是一项简单的任务,但一旦我得到了,我将如何从c#code运行下面的命令
谁能帮帮我吗
编辑:请注意,用户将始终输入serviceApplication exe文件而不是任意文件您可以查看
如果您想自己从头开始,可以查看,它只需添加所需的注册表项:
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);
}
你应该调查一下。您可能想尝试以下方法:
sc create TestService binPath= "C:\MyExePathWhichIsToBeRunAsWindowService"
Process.Start("sc", String.Format("create \"{0}\" binPath=\"{1}\"", serviceName, binPath));
为什么不使用
Process.Start从应用程序运行sc create
?我认为您不能将任意exe文件作为服务运行。它需要是一个服务应用程序。还是我误解了这个问题?@ThorstenDittmar用户将始终输入服务应用程序..而不是任意文件。现在怎么办?