2013-10-08 25 views
0

我有一個應用程序,其中PC(其他和自己)被添加爲服務器使用IP地址和狀態設置爲'正在運行'。在DB中創建一個相同的條目。如何解決C#中的InvalidOperationException?

假設我已經添加了2個服務器10.51.1.159和10.51.1.136,刷新狀態爲10.51.1.159是「正在運行」(因爲它是本地的),10.51.1.136的狀態是「錯誤」(因爲它是遠程)。當我嘗試調試時,10.51.1.136引發了以下異常。

遠程服務器拋出異常InvalidOperationException System.InvalidOperationException: Cannot open notifyservice service on computer '10.51.1.136'. ---> System.ComponentModel.Win32Exception: Access is denied

,這是否是如下代碼:

static public void GetServerStatus(DataTable dt, string service) 
{ 
    GetServerStatusColumns(dt); 

    // get the service status for each server in the datatable 
    ServiceController control = new ServiceController(); 
    foreach (DataRow row in dt.Rows) 
    { 
     try 
     { 
      control.ServiceName = service; 
      control.MachineName = row["SERVER_ADDRESS"].ToString(); 
      string status = control.Status.ToString(); // This is not set for 10.51.1.136, jumped to catch block after this. 
      row["SERVICE_STATUS_ID"] = Convert.ToInt32(Enum.Format(control.Status.GetType(), control.Status, "d")) - 1; 
      row["SERVICE_STATUS"] = status; 

      if (status == "Running") 
      { 
       row["SERVICE_CANSTART"] = false; 
       row["SERVICE_CANSTOP"] = true; 
      } 
      else 
      { 
       row["SERVICE_CANSTART"] = true; 
       row["SERVICE_CANSTOP"] = false; 
      } 
     } 
     catch (InvalidOperationException exp) 
     { 
      row["SERVICE_STATUS_ID"] = 5; // stopped 
      row["SERVICE_STATUS"] = "Error"; 
      string excep = Convert.ToString(exp); 
      row["SERVICE_CANSTART"] = false; 
      row["SERVICE_CANSTOP"] = false; 
     } 
    } 
} 

我檢查,如果防火牆阻止,但它似乎並不認爲防火牆的問題。 請幫助我。 在此先感謝。

回答

1

「訪問被拒絕」是指您的應用程序正在運行的帳戶無權管理其他計算機上的服務?

您是否在本地帳戶上運行?然後轉到域帳戶(在其他服務器上獲得權限)。

+0

是的我正在管理員帳戶上運行。 – Archana

+0

***本地***管理員帳戶? – jgauffin

+0

如何獲得在其他計算機上管理服務的權限?你能幫助我嗎? – Archana

相關問題