gpt4 book ai didi

visual-studio-2010 - 在 C# 中打印到网络打印机

转载 作者:行者123 更新时间:2023-12-01 04:09:19 26 4
gpt4 key购买 nike

我试图在 VS2010 中通过 C# 打印到网络服务,但在让它工作时遇到了困难。如果我使用“打印”动词插入,它可以正常打印,但只能打印到默认打印机。我正在使用 PrintTo Verb 尝试指定打印机。在我使用 print 动词的情况下,在我将默认打印机更改为其他打印​​机后,我成功地可以打印到我尝试使用 printto 动词打印的同一网络打印机。这是我目前使用的代码。任何帮助将不胜感激。

    private string FindPrinter(string printerName)
{
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection printers = searcher.Get();

foreach (ManagementObject printer in printers)
{
if (!String.IsNullOrEmpty(printer.Properties["PortName"].Value.ToString()))
{
return printerName = string.Format(@"\\{0}\{1}", printer.Properties["PortName"].Value.ToString(), printerName);
}
}

return printerName;
}

private void Print(string fileName, string printerName)
{
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = printerName;
if (ps.IsValid)
{
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName);
using (PrintDialog pd = new PrintDialog())
{
pd.ShowDialog();

printerName = this.FindPrinter(pd.PrinterSettings.PrinterName);
if (printerName.IndexOf(@"\\") == 0)
{

processStartInfo.Verb = "PrintTo";
processStartInfo.Arguments = printerName;
}
else
{
processStartInfo.Verb = "print";
}
}

processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Process printProcess = new Process();
printProcess.StartInfo = processStartInfo;
bool printStarted = printProcess.Start();
MessageBox.Show(string.Format("{0} printed to {1}", fileName, printerName), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show(string.Format("{0} printer does not exist. Please contact technical support.", printerName), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

最佳答案

只使用动词 PrintTo 和
使用双引号引用 printerName

processStartInfo.Verb = "PrintTo";
processStartInfo.Arguments = "\"" + printerName + "\"";

关于visual-studio-2010 - 在 C# 中打印到网络打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7322114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com