2011-09-06 15 views
4

VS2010의 C#을 통해 네트워크 servia로 인쇄하려고하는데 문제가 발생하여 작동하지 않습니다. "인쇄"Verb insted를 사용하면 기본 프린터에만 인쇄되지만 괜찮습니다. PrintTo Verb를 사용하여 프린터를 지정하려고합니다. 제 경우에는 인쇄 동사를 사용하여 기본 프린터를 다른 프린터로 변경 한 후 printto 동사를 사용하여 인쇄하려고하는 동일한 네트워크 프린터로 성공적으로 인쇄 할 수 있습니다. 다음은 현재 사용중인 코드입니다. 어떤 도움이라도 대단히 감사하겠습니다.C#의 네트워크 프린터로 인쇄

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

답변

2

에만 동사 PrintTo를 사용하고 는 printerName를

processStartInfo.Verb = "PrintTo"; 
processStartInfo.Arguments = "\"" + printerName + "\""; 
을 인용 따옴표를 사용
관련 문제