2013-02-02 2 views
1

누군가가 호스트 이름, 사용자 이름 및 암호를 사용하여 원격 시스템에서 Win32_ComputerSystem WMI 클래스의 개체를 검색하는 방법에 대한 몇 가지 예제 C# 코드가 있습니까?Win32_ComputerSystem 클래스 원격 요청

+0

에 반환로로드 방법으로 쿼리를 얻기. http://msdn.microsoft.com/en-us/library/w7sx1w4f.aspx –

답변

1

연결 :

try 
     { 
      rcOptions = new ConnectionOptions(); 
      rcOptions.Authentication = AuthenticationLevel.Packet; 
      rcOptions.Impersonation = ImpersonationLevel.Impersonate; 
      rcOptions.EnablePrivileges = true; 
      rcOptions.Username = servername + @"\" + username; 
      rcOptions.Password = password; 

      mScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", servername), rcOptions); 
      mScope.Connect(); 
      if (mScope.IsConnected == true) { MessageBox.Show("Connection Succeeded", "Alert"); } else { MessageBox.Show("Connection Failed", "Alert"); } 
      if (mScope.IsConnected == true) { lblConnectionStateWarning.Text = "Connected"; } else { lblConnectionStateWarning.Text = "Disconnected"; } //I have a label that displays connectionstate, you can leave that out 

     } 
     catch (Exception ex) { MessageBox.Show(ex.Message); } 

것은 dictonary &는 그냥 ManagementScope 클래스에 대한 MSDN 라이브러리 문서의 예제 코드를 보면 목록보기

private void FindWMI(string servername, string classSelection, ConnectionOptions rcOptions, ListView listView) 
     { 
      try 
      { 
       var dct = new Dictionary<string, string>(); 
       List<ListViewItem> itemsList = new List<ListViewItem>(); 
       oQuery = new ObjectQuery("select * from " + classSelection); 
       moSearcher = new ManagementObjectSearcher(mScope, oQuery); 
       moCollection = moSearcher.Get(); 

      Invoke(new MethodInvoker(() => 
      { 
       listView.Items.Clear(); 
      })); 

      foreach (ManagementObject mObject in moCollection) 
      { 
       if (mObject != null) 
       { 
        foreach (PropertyData propData in mObject.Properties) 
        { 
         if (propData.Value != null && propData.Value.ToString() != "" && propData.Name != null && propData.Name != "") 
          dct[propData.Name] = propData.Value.ToString(); 
//Don't forget this, when the result is an array, you want all the strings in that array.. 
if (propData.Value is Array) { dct[propData.Name] = ""; foreach (string stringArray in (string[])propData.Value) { dct[propData.Name] += stringArray + "\n"; } } 
        } 
       } 
      } 

      foreach (KeyValuePair<string, string> listItem in dct) 
      { 
       ListViewItem lstItem = new ListViewItem(listItem.Key); 
       lstItem.SubItems.Add(listItem.Value); 
       itemsList.Add(lstItem); 
      } 

      Invoke(new MethodInvoker(() => 
      { 
       listView.Items.AddRange(itemsList.ToArray()); 
      })); 
     } 
     catch (Exception) { } 
    } 
+0

@TWT이 예제를 사용하려는 경우 때때로 속성 값이 배열을 반환하므로 추가해야합니다. (look at edit) if (propData.Value가 배열 인 경우) {dct [propData.Name] = ""; foreach (string []) (string []) propData.Value) {dct [propData.Name] + = stringArray + "\ n"; }} –

관련 문제