C# 我是否有权启动/停止windows服务?
在C#中是否有办法确定我是否有权启动和停止windows服务 如果我的进程在网络服务帐户下运行,并且我尝试停止服务,我将获得“拒绝访问”异常,这是正常的,但我希望在尝试操作之前能够确定我是否获得授权 我正在尝试改进如下代码:C# 我是否有权启动/停止windows服务?,c#,windows-services,C#,Windows Services,在C#中是否有办法确定我是否有权启动和停止windows服务 如果我的进程在网络服务帐户下运行,并且我尝试停止服务,我将获得“拒绝访问”异常,这是正常的,但我希望在尝试操作之前能够确定我是否获得授权 我正在尝试改进如下代码: var service = new ServiceController("My Service"); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSe
var service = new ServiceController("My Service");
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
private bool AutorizedToStopWindowsService()
{
try
{
// Try to find one of the well-known services
var wellKnownServices = new[]
{
"msiserver", // Windows Installer
"W32Time" // Windows Time
};
var services = ServiceController.GetServices();
var service = services.FirstOrDefault(s => s.ServiceName.In(wellKnownServices) && s.Status.In(new[] { ServiceControllerStatus.Running, ServiceControllerStatus.Stopped }));
// If we didn't find any of the well-known services, we'll assume the user is not autorized to stop/start services
if (service == null) return false;
// Get the current state of the service
var currentState = service.Status;
// Start or stop the service and then set it back to the original status
if (currentState == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(5));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(5));
}
else
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(5));
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(5));
}
// If we get this far, it means that we successfully stopped and started a windows service
return true;
}
catch
{
// An error occurred. We'll assume it's due to the fact the user is not authorized to start and stop services
return false;
}
}
例如:
if (AmIAuthorizedToStopWindowsService())
{
var service = new ServiceController("My Service");
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
}
更新
像这样的东西怎么样:
var service = new ServiceController("My Service");
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
private bool AutorizedToStopWindowsService()
{
try
{
// Try to find one of the well-known services
var wellKnownServices = new[]
{
"msiserver", // Windows Installer
"W32Time" // Windows Time
};
var services = ServiceController.GetServices();
var service = services.FirstOrDefault(s => s.ServiceName.In(wellKnownServices) && s.Status.In(new[] { ServiceControllerStatus.Running, ServiceControllerStatus.Stopped }));
// If we didn't find any of the well-known services, we'll assume the user is not autorized to stop/start services
if (service == null) return false;
// Get the current state of the service
var currentState = service.Status;
// Start or stop the service and then set it back to the original status
if (currentState == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(5));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(5));
}
else
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(5));
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(5));
}
// If we get this far, it means that we successfully stopped and started a windows service
return true;
}
catch
{
// An error occurred. We'll assume it's due to the fact the user is not authorized to start and stop services
return false;
}
}
不是真的。您可以尝试推理您的帐户是否具有权限(如管理员或域管理员组的成员),这可能足以满足您的情况 Windows中的权限可以在非常精细的对象/操作上设置,因此任何特定成员资格都不必保证您的帐户对特定对象具有特定操作的权限,即使它对其他对象具有类似/相同操作的权限 处理的方法是尝试操作并处理异常
如果您很好-请在异常消息(或链接到)中提供详细说明,说明帐户需要哪些权限,您的特定情况下有哪些良好做法,以及可能的廉价解决方案(即“此程序要求YYYY具有XXXX权限。您可以作为测试管理员运行,但不建议用于生产”).我实现了我在更新的问题中描述的AutorizedToStopWindowsService()方法,它运行得很好。我认为您可以执行一些基本的权限检查,例如,确定您的进程是否以管理员身份运行;但这并不总是确定的。我认为,如果您没有适当的权限,您唯一能做的100%准确的事情就是尝试停止服务并捕获异常。@CodingGorilla是的。我怀疑是否有办法“锁定”操作系统,以防止在测试帐户是否可以停止服务和实际停止服务之间发生权限更改。我找到了一个“iAdministrator”方法,该方法返回一个布尔值,指示用户是否是本地管理员角色的成员,但这可能是过度的。通常,您需要是管理员才能控制服务,因此检查本地管理员权限应该是有效的。停止并重新启动“Windows Time”服务(或任何其他已知服务)如何?如果成功,我们假定当前用户已被授权,否则我们假定他未被授权。这是个好主意吗?是否有一个“众所周知”的服务,我们可以假定它将出现在大多数系统上?