在 C# 中打印到网络打印机

发布于 2024-12-03 02:40:20 字数 2647 浏览 2 评论 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);
        }
    }

I am attempting to print to a network servia via C# in VS2010 but have run into difficulties getting it to work. If I use the "print" Verb insted it prints fine but only to the default printer. I am using the PrintTo Verb to try and specify a printer. In my case using print verb I successfully can print to the same network printer that I am trying to print to using the printto verb after I change my default printer to a different printer. Here is the code I am currently using. Any help would be greatly appreciated.

    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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2024-12-10 02:40:20

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

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

only use verb PrintTo and
use the double quotes to quote the printerName

processStartInfo.Verb = "PrintTo";
processStartInfo.Arguments = "\"" + printerName + "\"";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文