C#启动windows服务报错

最近用C#操作windows服务遇到“无法打开计算机 上的服务控制管理器。此操作可能需要其他特权。。”的问题。程序是在win7的vs2010上写的,在win7上运行没有错误,在xp上就出现这个错误。具体错误信息如下所示:

 

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
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 调试器,
而不是由此对话框处理。
 
解决方法:
 
程序如下:
ServiceController cs = new ServiceController();
 cs.MachineName =localhost; 
 cs.ServiceName = serverName;
 cs.start();
 cs.Refresh();
 
将上述内容改为:
 ServiceController cs = new ServiceController(serverName);
 cs.start();
 cs.Refresh();
就没有问题了。个人理解是win7的权限做的比较细,xp系统没有对应的概念,所以指定MachineName的时候就找不到相应机器的服务管理器。没有指定的默认就是本机吧。
 
错误尝试:
1、 将localhost改为 “127.0.0.1”,错误依然存在。
2、 增加权限,在启动服务方法前增加权限,都不可以。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章