最近用C#操作windows服务遇到“无法打开计算机 上的服务控制管理器。此操作可能需要其他特权。。”的问题。程序是在win7的vs2010上写的,在win7上运行没有错误,在xp上就出现这个错误。具体错误信息如下所示:
请参见此消息的结尾。
************** 异常文本 **************
System.InvalidOperationException: 无法打开计算机“127.0.0.1”上的服务控制管理器。此操作可能需要其他特权。 ---> System.ComponentModel.Win32Exception: RPC 服务器不可用。
--- 内部异常堆栈跟踪的结尾 ---
在 System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess)
在 System.ServiceProcess.ServiceController.GetDataBaseHandleWithConnectAccess()
在 System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
在 System.ServiceProcess.ServiceController.GenerateStatus()
在 System.ServiceProcess.ServiceController.get_Status()
在 SystemTrayApp.Form1.getServiceStatus()
在 SystemTrayApp.Form1.Form1_Load(Object sender, EventArgs e)
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3643 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework2.0.50727/mscorlib.dll
SystemTrayApp
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///C:/Documents%20and%20Settings/Administrator/桌面/SystemTrayApp.exe
----------------------------------------
System.Windows.Forms
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3637 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3643 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3639 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.ServiceProcess
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.ServiceProcess/2.0.0.0__b03f5f7f11d50a3a/System.ServiceProcess.dll
----------------------------------------
System.ServiceProcess.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.ServiceProcess.resources/2.0.0.0_zh-CHS_b03f5f7f11d50a3a/System.ServiceProcess.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3643 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework2.0.50727/mscorlib.dll
************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。
例如:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。
cs.MachineName =localhost;
cs.ServiceName = serverName;
cs.Refresh();
cs.Refresh();