title: Retrieving Disk information from remote server C#
body: <p>Hi we are working on C# framework v4.0 asp.net mvc  application on a distributed structure. We are using Server1 for image Server2 for mysql database and Server3 for web. My task is to get free disk space and total disk space from all three servers and to display on web admin page. I know how to display disk space for localhost using WMI. I want to display Server1 and Server3 disk information which have static ip (All three server have remote desktop and sharing enabled). but i get </p>
  
  <pre><code>Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  </code></pre>
  
  <p>here is my code</p>
  
  <pre><code>ManagementScope scope = new ManagementScope("\\\\Server IP\\root\\cimv2");
  scope.Connect();
  
  ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
  SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
  
  ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  
  ManagementObjectCollection queryCollection = searcher.Get();
  
  ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
  ManagementObjectCollection queryCollection1 = searcher1.Get();
  
  foreach (ManagementObject m in queryCollection)
  {
      // Display the remote computer information
      Console.WriteLine("Computer Name : {0}",
                        m["csname"]);
      Console.WriteLine("Windows Directory : {0}",
                        m["WindowsDirectory"]);
      Console.WriteLine("Operating System: {0}",
                        m["Caption"]);
      Console.WriteLine("Version: {0}", m["Version"]);
      Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
      Console.WriteLine();
  
  }
  
  foreach (ManagementObject mo in queryCollection1)
  {
      Console.WriteLine("  Disk Name : {0}", mo["Name"]);
      Console.WriteLine("   Disk Size : {0}", mo["Size"]);
      Console.WriteLine("  FreeSpace : {0}", mo["FreeSpace"]);
      Console.WriteLine("  Disk DeviceID : {0}", mo["DeviceID"]);
      Console.WriteLine("  Disk VolumeName : {0}", mo["VolumeName"]);
      Console.WriteLine("  Disk SystemName : {0}", mo["SystemName"]);
      Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
      Console.WriteLine();
  }
  Console.ReadLine();
  </code></pre>
  
  <p>but when compile goes to line scope.Connect(); 
  then it throws exception access denied</p>
  
  <p>I have tried <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa389284(v=vs.85).aspx" rel="nofollow noreferrer">this article using Dcomcnfg.exe security</a> but still cant get rid of this exception</p>
  
  <p>i have also tried</p>
  
  <pre><code>ConnectionOptions options = new ConnectionOptions();
  options.Username = @"Admin-PC\username";
  options.Password = @"password";
  options.Impersonation = ImpersonationLevel.Impersonate;
  options.Authentication = AuthenticationLevel.Connect;
  options.EnablePrivileges = true;
  options.Timeout = new TimeSpan(0, 60, 0);
  ManagementScope scope = new ManagementScope("\\\\IP\\root\\cimv2", options);
  </code></pre>