title: How to print a test page and capture the return value for a non default printer on a remote server using WMI's PrintTestPage method?
body: <p>I'm using the System.Management namespace along with WMI to install printers/ports on a remote server. Once the printer/port is installed, I'd like to print a test page, capture whether or not printing the test page was (theoretically) successful, and display the result to the user. I've got everything working up until the point of printing the test page.</p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/858442/can-i-print-a-standard-test-page-in-code-behind-in-net-3-5">this article</a>, as well as <a href="https://stackoverflow.com/questions/3710196/how-to-print-test-page-on-default-printer">this article</a>, on the subject, but neither one quite seems to be doing the trick because I'm not seeing how to apply them to the current printer I've just installed.</p>
  
  <p>Anyway, here's the code I'm working with to install the printer (the port code is similar and probably not necessary...there's also additional code I'm using to log in to the remote server, but as long as you know that I'm creating a ManagementScope object with a ConnectionOptions object attached, there's no reason to show that code either):</p>
  
  <pre><code>foreach (Printers p in lstPrinters)
  {
      search = printerQuery + "'" + p.PrinterName + "'";
  
      query = new SelectQuery(search);
  
      try
      {
          using (var searcher = new ManagementObjectSearcher(scope, query))
          {
              ManagementObjectCollection printers = searcher.Get();
  
              ObjectGetOptions oPrinterOptions =
                  new ObjectGetOptions(null, TimeSpan.MaxValue, true);
              ManagementPath mPathPrinter =
                  new ManagementPath("Win32_Printer");
              ManagementClass mClassPrinter =
                  new ManagementClass(scope, mPathPrinter, oPrinterOptions);
              ManagementObject mNewPrinter = mClassPrinter.CreateInstance();
  
              mNewPrinter["Comment"] = p.Comment;
              mNewPrinter["DeviceID"] = p.DeviceID;
              mNewPrinter["DriverName"] = p.DriverName;
              mNewPrinter["Location"] = p.Location;
              mNewPrinter["Network"] = p.Network;
              mNewPrinter["PortName"] = p.PortName;
              mNewPrinter["Shared"] = p.Shared;
              mNewPrinter["ShareName"] = p.ShareName;
  
              // If no ports are returned, add the port.
              // Otherwise, update the current port object.
              PutOptions poPutPrinter = new PutOptions();
              poPutPrinter.Type = PutType.UpdateOrCreate;
              poPutPrinter.UseAmendedQualifiers = true;
              mNewPrinter.Put(poPutPrinter);
          }
      }
  }
  </code></pre>
  
  <p>I thought I would be able to do something like this...which is similar to an example in second article link I listed above:</p>
  
  <pre><code>ManagementBaseObject retValue = mNewPrinter.InvokeMethod("PrintTestPage", null);
  
  if (Convert.ToInt32(retValue["ReturnValue"]) == 0)
  {
      MessageBox.Show("Test page printed successfully.");
  }
  </code></pre>
  
  <p>In theory, at least in my head, the above would ensure that, by assigning my <code>mNewPrinter</code> ManagementObject variable to the InvokeMethod, the PrintTestMethod is linked to that printer on that specific server.</p>
  
  <p>Thanks...</p>