2012-11-05 17 views
5

सी # में मैं सभी प्रिंटर सिस्टम चल रहा पर स्थापित ड्राइवरों की एक सूची प्राप्त होगा की सूची प्राप्त करने के लिए, Windows "add printer" wizard की तरह:सभी उपलब्ध प्रिंटर ड्राइवर (प्रिंटर जोड़ें विज़ार्ड की तरह)

मैं सूची कर लिया है पहले से ही प्रिंटर स्थापित हैं, लेकिन मैं सिस्टम पर उपलब्ध ड्राइवरों को कैसे सूचीबद्ध करूं?

+0

यह विज़ार्ड करता है * नहीं * सूची स्थापित प्रिंटर ड्राइवर, यह चालकों कि * * स्थापित किया जा सकता शामिल हैं। इस तरह की कार्यक्षमता सेटअप एपीआई में दफनाया गया है। .NET से उपयोग करना बहुत मुश्किल है, पिनवोक खराब है। और यह सामान्य रूप से उपयोग करने के लिए * एक साधारण एपीआई नहीं है। http://msdn.microsoft.com/en-us/library/windows/hardware/ff553567%28v=vs.85%29.aspx –

+0

आपके उत्तर के लिए बहुत बहुत धन्यवाद। मुझे लगता है कि मैं इसके बजाय बचाना चाहूंगा ... –

उत्तर

0

शायद यह आपकी मदद कर सकता है: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.installedprinters.aspx इसके साथ आप स्थापित प्रिंटर प्राप्त कर सकते हैं।

+1

यह उपलब्ध प्रिंटर की एक सूची को पुनर्प्राप्त करता है, उपलब्ध ड्राइवर नहीं। – larsmoa

+0

या आप स्थापित प्रिंटर नहीं चाहते हैं, लेकिन सभी उपलब्ध हैं? – Burucsb

+0

मुझे खेद है ... – Burucsb

0

इस कोड विश्लेषण करता है स्थापित प्रिंटर ड्राइवर:

public struct DRIVER_INFO_2 
{ 
    public uint cVersion; 
    [MarshalAs(UnmanagedType.LPTStr)] public string pName; 
    [MarshalAs(UnmanagedType.LPTStr)] public string pEnvironment; 
    [MarshalAs(UnmanagedType.LPTStr)] public string pDriverPath; 
    [MarshalAs(UnmanagedType.LPTStr)] public string pDataFile; 
    [MarshalAs(UnmanagedType.LPTStr)] public string pConfigFile; 
} 


public static class EnumeratePrinterDriverNames 
{ 
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern int EnumPrinterDrivers(String pName, String pEnvironment, uint level, IntPtr pDriverInfo, 
     uint cdBuf, ref uint pcbNeeded, ref uint pcRetruned); 

    public static IEnumerable<string> Enumerate() 
    { 
     const int ERROR_INSUFFICIENT_BUFFER = 122; 

     uint needed = 0; 
     uint returned = 0; 
     if (EnumPrinterDrivers(null, null, 2, IntPtr.Zero, 0, ref needed, ref returned) != 0) 
     { 
      //succeeds, but shouldn't, because buffer is zero (too small)! 
      throw new ApplicationException("EnumPrinters should fail!"); 
     } 

     int lastWin32Error = Marshal.GetLastWin32Error(); 
     if (lastWin32Error != ERROR_INSUFFICIENT_BUFFER) 
     { 
      throw new Win32Exception(lastWin32Error); 
     } 

     IntPtr address = Marshal.AllocHGlobal((IntPtr) needed); 
     try 
     { 
      if (EnumPrinterDrivers(null, null, 2, address, needed, ref needed, ref returned) == 0) 
      { 
       throw new Win32Exception(Marshal.GetLastWin32Error()); 
      } 

      var type = typeof (DRIVER_INFO_2); 
      IntPtr offset = address; 
      int increment = Marshal.SizeOf(type); 

      for (uint i = 0; i < returned; i++) 
      { 
       var di = (DRIVER_INFO_2) Marshal.PtrToStructure(offset, type); 
       offset += increment; 

       yield return di.pName; 
      } 
     } 
     finally 
     { 
      Marshal.FreeHGlobal(address); 
     } 
    } 
} 
संबंधित मुद्दे