你如何设置PrintDocument.PrinterSettings.PrinterName
为默认打印机?
我不是在谈论在操作系统中设置默认打印机.相反,我说的是设置PrintDocument对象,以便它打印到默认打印机.
Ben*_*yne 11
如果我理解正确,您希望能够将PrinterName重置为默认打印机(1)而无需重新创建PrintDocument , (2)在您可能已经将其设置为其他内容之后, 或者 (3)当自PrintDocument首次创建以来,默认打印机可能已更改 (因此您不能依赖于在初始构建后简单地缓存目标实例提供的默认值).
在这种情况下,搜索" C#get default printer name "会在stackoverflow上发布以下优秀文章:在.NET中获取默认打印机的最佳方法是什么
建立在最高投票答案中提供的样本,并考虑到您已经预先存在PrintDocument
一些您不想重新创建的设置; 您可以创建PrinterSettings
该类的新实例,仅用于复制默认打印机名称.
// Create a new instance of the PrinterSettings class, which
// we will only use to fetch the default printer name
System.Drawing.Printing.PrinterSettings newSettings = new System.Drawing.Printing.PrinterSettings();
// Copy the default printer name from our newSettings instance into our
// pre-existing PrintDocument instance without recreating the
// PrintDocument or the PrintDocument's PrinterSettings classes.
existingPrintDocumentInstance.PrinterSettings.PrinterName = newSettings.PrinterName;
Run Code Online (Sandbox Code Playgroud)
您可以查看链接帖子以了解WMI等替代技术,但我认为这是最简单,最干净的解决方案.
它会自动初始化为默认打印机.没做什么.