Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2010 用C语言打印到网络打印机_Visual Studio 2010_C# 4.0 - Fatal编程技术网

Visual studio 2010 用C语言打印到网络打印机

Visual studio 2010 用C语言打印到网络打印机,visual-studio-2010,c#-4.0,Visual Studio 2010,C# 4.0,我试图在VS2010中通过C打印到网络服务,但在使其工作时遇到困难。如果我使用print动词insted,它可以很好地打印,但只能打印到默认打印机。我正在使用PrintTo动词尝试指定打印机。在我使用print verb的情况下,在我将默认打印机更改为其他打印机后,我可以使用printto verb成功地打印到我尝试打印到的同一台网络打印机。这是我目前使用的代码。任何帮助都将不胜感激 private string FindPrinter(string printerName) {

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

    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 + "\"";