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

标签 visual-studio-2010 c#-4.0

我试图在 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/

相关文章:

sql - 将sql server数据库中的所有数据显示到一个表中

visual-studio-2010 - 提交通过 NuGet 添加的库

c - "char"类型的参数与 char* 类型的参数不兼容

multithreading - C# TPL 任务 - 一次有多少

c# - 单击下一页后,在上一页上找到取消选中的复选框

c# - C# 中的 cURL 调用

mysql - 我应该如何在我的 SQL 查询中实现和 IF 语句?

c++ - 在调试器中查看来自 DLL 的 pimpl

c++ - 结构数组的初始化列表的行为

c# - 为什么从 C# 4.0 开始使用的 COM 库需要大量使用动态类型?