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

前端 未结 7 1335
生来不讨喜
生来不讨喜 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条回答
  •  被撕碎了的回忆
    2020-12-07 13:34

    Another approach is using WMI (you'll need to add a reference to the System.Management assembly):

    public static string GetDefaultPrinterName()
    {
        var query = new ObjectQuery("SELECT * FROM Win32_Printer");
        var searcher = new ManagementObjectSearcher(query);
    
        foreach (ManagementObject mo in searcher.Get())
        {
            if (((bool?) mo["Default"]) ?? false)
            {
                return mo["Name"] as string;
            }
        }
    
        return null;
    }
    

提交回复
热议问题