简体   繁体   English

在代码中从“手动”状态启动WebClient Windows服务

[英]Start WebClient Windows Service from Manual state in code

The WebClient Windows Service is installed and set to Manual by default; 默认情况下, WebClient Windows服务已安装并设置为“手动”。 due to my customer's IT restrictions I am unable to change this to Automatic. 由于客户的IT限制,我无法将其更改为“自动”。

When the service is stopped and I try to access files with Directory.EnumerateDirectories I get a the exception: 当服务停止并且我尝试使用Directory.EnumerateDirectories访问文件时,出现一个异常:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.IO.DirectoryNotFoundException'的未处理异常

Additional information: Could not find a part of the path '\\mysever\\myfolder'. 附加信息:找不到路径'\\ mysever \\ myfolder'的一部分。

When the WebClient service is started this works fine. 当启动WebClient服务时,它可以正常工作。

Accessing the path using Explorer works fine as the WebClient service is started as part of this request. 使用Web浏览器作为该请求的一部分时,使用Explorer可以很好地访问路径。

From code, how can I tell Windows that I want to access the WebClient service so it should start it? 从代码中,如何告诉Windows我想访问WebClient服务,以便启动它?

I have the following (working) code, but I am unsure if this requires Administrator permission or if there is any better way to do this: 我有以下(工作)代码,但是不确定是否需要管理员权限,或者是否有更好的方法来执行此操作:

using (ServiceController serviceController = new ServiceController("WebClient"))
{
    serviceController.Start();
    serviceController.WaitForStatus(ServiceControllerStatus.Running);
}

Effectively all I want to do is to execute the command net start WebClient , is the above code the cleanest way to do this and is there any security restrictions I need to be aware of in a locked down environment? 实际上,我要做的就是执行命令net start WebClient ,以上代码是执行此操作的最干净的方法,并且在锁定环境中是否需要了解任何安全限制?

I have checked the MSDN for ServiceController.Start Method and it doesn't seem say if users must be administrator or not. 我已经检查了MSDN上的ServiceController.Start方法 ,似乎没有说用户是否必须是管理员。

You need administrative privileges. 您需要管理权限。

You can test this out with the below code in a console application on a computer with the WebClient service turned off. 您可以在WebClient服务关闭的情况下,在计算机上的控制台应用程序中使用以下代码进行测试。 Running without administrative privileges gives you "cannot start service on computer '.'" 在没有管理特权的情况下运行会使您“无法在计算机'。'上启动服务”。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM