C# 在代码中从手动状态启动WebClient Windows服务
C# 在代码中从手动状态启动WebClient Windows服务,c#,windows-services,webdav,C#,Windows Services,Webdav,WebClientWindows服务已安装,默认设置为手动;由于客户的IT限制,我无法将此更改为自动 当服务停止时,我尝试使用目录访问文件。EnumerateDirectory我得到一个异常: “System.IO.DirectoryNotFoundException”类型的未处理异常 发生在mscorlib.dll中 其他信息:找不到路径的一部分 “\mysever\myfolder” 当WebClient服务启动时,这一切正常 使用Explorer访问路径可以正常工作,因为WebClient
WebClient
Windows服务已安装,默认设置为手动;由于客户的IT限制,我无法将此更改为自动
当服务停止时,我尝试使用目录访问文件。EnumerateDirectory
我得到一个异常:
“System.IO.DirectoryNotFoundException”类型的未处理异常
发生在mscorlib.dll中
其他信息:找不到路径的一部分
“\mysever\myfolder”
当WebClient服务启动时,这一切正常
使用Explorer访问路径可以正常工作,因为WebClient服务是作为此请求的一部分启动的
从代码中,我如何告诉Windows我想访问WebClient服务,以便它启动它
我有以下(工作)代码,但我不确定这是否需要管理员权限,或者是否有更好的方法来执行此操作:
using (ServiceController serviceController = new ServiceController("WebClient"))
{
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
}
实际上,我只想执行命令net start WebClient
,上面的代码是最干净的方法吗?在锁定的环境中,是否有任何安全限制需要注意
我已经检查了MSDN,但它似乎没有说明用户是否必须是管理员。您需要管理员权限 您可以在关闭WebClient服务的计算机上的控制台应用程序中使用以下代码测试这一点。 在没有管理权限的情况下运行会使您“无法在计算机上启动服务”
static void Main(string[] args)
{
string serviceToRun = "WebClient";
using (ServiceController serviceController = new ServiceController(serviceToRun))
{
Console.WriteLine(string.Format("Current Status of {0}: {1}", serviceToRun, serviceController.Status));
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
Console.WriteLine(string.Format("Starting {0}", serviceToRun));
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20));
Console.WriteLine(string.Format("{0} {1}", serviceToRun, serviceController.Status));
}
}
Console.ReadLine();
}