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、 增加權限,在啓動服務方法前增加權限,都不可以。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章