The process always prints the default document.

I have a problem choosing a printer to print my document.

My code is:

var filename = @"C:\Users\I\Desktop\test.doc"; PrintDialog pd = new PrintDialog(); pd.PrinterSettings =new PrinterSettings(); if (DialogResult.OK == pd.ShowDialog(this)) { Process objP = new Process(); objP.StartInfo.FileName = filename; objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window. objP.StartInfo.Verb ="print"; objP.StartInfo.Arguments ="/p /h \"" + filename + "\" \"" + pd.PrinterSettings.PrinterName + "\""; objP.StartInfo.CreateNoWindow = false; //true;//!! Don't create a Window. objP.Start(); //!! Start the process !!// objP.CloseMainWindow(); } 

and no matter what I choose, process will always use the default printer, no matter what the value of pd.PrinterSettings.PrinterName .

What is wrong with my code?

+4
source share
2 answers

You might want to use "PrintTo" instead of "print" for the verb. You have already set objP.FileName to the file name, so there is no need to complicate the arguments. Skip only the printer name.

 var filename = @"C:\Users\I\Desktop\test.doc"; PrintDialog pd = new PrintDialog(); pd.PrinterSettings =new PrinterSettings(); if (DialogResult.OK == pd.ShowDialog(this)) { Process objP = new Process(); objP.StartInfo.FileName = filename; objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window. objP.StartInfo.Verb ="PrintTo"; objP.StartInfo.Arguments = pd.PrinterSettings.PrinterName; objP.StartInfo.CreateNoWindow = false; //true;//!! Don't create a Window. objP.Start(); //!! Start the process !!// objP.CloseMainWindow(); } 
+3
source

Try changing pd.PrinterSettings =new PrinterSettings(); to read something like this:

 pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings; 

By default, when you create an instance of printer settings, it returns the default printer name only fyi ... you can try something like this

 //sudu code foreach(string strPrinter in PrinterSettings.InstalledPrinters) { // or unless you know the name of the printer then skip this and assign it to the code above } 
+2
source

Source: https://habr.com/ru/post/1385229/


All Articles