如何在Windows中启动/停止索引服务
问题描述:
我想使用C#在Windows中启动/停止索引服务.怎么做?
启动服务后,我想创建目录和范围..
I would like to start/stop indexing services in windows using C#. How to do it?
After starting service I would like to create catalog and scope ..
答
请看以下示例: ^ ]
Have a look at this example: Start, Stop and Restart Windows Service [C#][^]
start
start
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
停止
stop
public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch
{
// ...
}
}