title: How to get all printer name in windows service?
body: <p>I want to get all printer name installed on computer and network.
  I used these function in below to get list of all printer. All these func. is working correctly in console and form application and i can get list of all printer but when i try to use these func. in windows service i can get only installed on the computer with out network. And also my service working under <code>LocalSystem</code> account.</p>
  
  <p><strong>Func 1:</strong></p>
  
  <pre><code>        WinsPool.PrinterEnumFlags Flags = WinsPool.PrinterEnumFlags.PRINTER_ENUM_CONNECTIONS | WinsPool.PrinterEnumFlags.PRINTER_ENUM_LOCAL;
          const int ERROR_INSUFFICIENT_BUFFER = 122;
          uint cbNeeded = 0;
          uint cReturned = 0;
          if (WinsPool.EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
          {
              return null;
          }
          int lastWin32Error = Marshal.GetLastWin32Error();
          if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
          {
              IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
              if (WinsPool.EnumPrinters(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
              {
                  WinsPool.PRINTER_INFO_2[] printerInfo2 = new WinsPool.PRINTER_INFO_2[cReturned];
                  int offset = pAddr.ToInt32();
                  Type type = typeof(WinsPool.PRINTER_INFO_2);
                  int increment = Marshal.SizeOf(type);
                  for (int i = 0; i &lt; cReturned; i++)
                  {
                      printerInfo2[i] = (WinsPool.PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                     string printerName =  printerInfo2[i].pPrinterName;
                      offset += increment;
                  }
                  Marshal.FreeHGlobal(pAddr);
              }
              lastWin32Error = Marshal.GetLastWin32Error();
          }
  </code></pre>
  
  <p><strong>Func 2:</strong></p>
  
  <pre><code>        foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
          {
              string printerName = printer;
          }
  </code></pre>
  
  <p><strong>Func 3:</strong></p>
  
  <pre><code>            ManagementObjectSearcher searcher = 
                  new ManagementObjectSearcher("root\\CIMV2", 
                  "SELECT * FROM Win32_Printer"); 
  
              foreach (ManagementObject queryObj in searcher.Get())
              {
                 string printerName = queryObj["Name"].ToString();
              }
  </code></pre>