MSDN是你的朋友。:)
如果您在该计算机上拥有管理员权限,则此代码将完美运行:
using System.ServiceProcess;
ServiceController sc = new ServiceController("YourService", "MachineName");
if (sc.Status.Equals(ServiceControllerStatus.Stopped) ||
sc.Status.Equals(ServiceControllerStatus.StopPending))
{
}
否则,正如另一条评论中提到的那样,如果您正在运行的帐户没有该计算机上的管理员权限,则可能需要使用模拟身份设置此项,如下所示:
string userName = "domain\\user";
string password = "whatever";
WindowsImpersonationContext adminContext = Impersonation.getWic(userName, password);
if (adminContext != null)
{
try
{
ServiceController sc = new ServiceController("YourService", "MachineName");
if (sc.Status.Equals(ServiceControllerStatus.Stopped) ||
sc.Status.Equals(ServiceControllerStatus.StopPending))
{
}
}
catch (Exception ex)
{
Console.Out.WriteLine("\nUnable to set profile to Mandatory:\n\t" + ex.Message);
Impersonation.endImpersonation();
adminContext.Undo();
}
finally
{
Impersonation.endImpersonation();
adminContext.Undo();
}
}
这是我的独立的模拟类。它有两个主要入口点:getWic()
和doImpersonation()
-- getWic()
将接受一个看起来像domain\user
或machinename\user
的用户名,并将其分解成其组成部分,然后将其传递给doImpersonation()
;而doImpersonation()
则接受已经分解好的部分,如果您已经这样做了并且不需要在getWic()
中编写代码。两者均返回WindowsImpersonationContext
。
using System;
using System.Data;
using System.Configuration;
using System.Security.Permissions;
using System.Security.Principal;
using System.Runtime.InteropServices;
[assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode = true)]
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
public class Impersonation
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (retVal == false)
else
}
public static void endImpersonation()
public static WindowsImpersonationContext getWic(string userNameStringFromTextbox, string password)
catch (Exception ex)
}
#region Errors
const int NO_ERROR = 0;
const int ERROR_ACCESS_DENIED = 5;
const int ERROR_ALREADY_ASSIGNED = 85;
const int ERROR_BAD_DEVICE = 1200;
const int ERROR_BAD_NET_NAME = 67;
const int ERROR_BAD_PROVIDER = 1204;
const int ERROR_CANCELLED = 1223;
const int ERROR_EXTENDED_ERROR = 1208;
const int ERROR_INVALID_ADDRESS = 487;
const int ERROR_INVALID_PARAMETER = 87;
const int ERROR_INVALID_PASSWORD = 1216;
const int ERROR_MORE_DATA = 234;
const int ERROR_NO_MORE_ITEMS = 259;
const int ERROR_NO_NET_OR_BAD_PATH = 1203;
const int ERROR_NO_NETWORK = 1222;
const int ERROR_SESSION_CREDENTIAL_CONFLICT = 1219;
const int ERROR_BAD_PROFILE = 1206;
const int ERROR_CANNOT_OPEN_PROFILE = 1205;
const int ERROR_DEVICE_IN_USE = 2404;
const int ERROR_NOT_CONNECTED = 2250;
const int ERROR_OPEN_FILES = 2401;
private struct ErrorClass
}
private static ErrorClass[] ERROR_LIST = new ErrorClass[] ;
private static string GetError(int errNum)
return "Error: Unknown, " + errNum;
}
#endregion
}
参考资料:
ServiceController类。
https://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicecontroller(v=vs.110).aspx
WindowsIdentity.Impersonate方法。
https://msdn.microsoft.com/zh-cn/library/w070t6ka(v=vs.110).aspx