What's the best way to get the default printer in .NET

前端 未结 7 1318
生来不讨喜
生来不讨喜 2020-12-07 12:58

I need to get the default printer name. I\'ll be using C# but I suspect this is more of a framework question and isn\'t language specific.

7条回答
  •  萌比男神i
    2020-12-07 13:46

    If you just want the printer name no advantage at all. But WMI is capable of returning a whole bunch of other printer properties:

    using System;
    using System.Management;
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                ObjectQuery query = new ObjectQuery(
                    "Select * From Win32_Printer " +
                    "Where Default = True");
    
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(query);
    
                foreach (ManagementObject mo in searcher.Get())
                {
                    Console.WriteLine(mo["Name"] + "\n");
    
                    foreach (PropertyData p in mo.Properties)
                    {
                        Console.WriteLine(p.Name );
                    }
                }
            }
        }
    }
    

    and not just printers. If you are interested in any kind of computer related data, chances are you can get it with WMI. WQL (the WMI version of SQL) is also one of its advantages.

提交回复
热议问题