when i run this code :
Process printjob = new Process();
printjob.StartInfo.FileName = "file.html";
printjob.StartInfo.UseShellExecute = true;
printjob.StartInfo.Verb = "print";
printjob.StartInfo.CreateNoWindow = true;
printjob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printjob.Start();
this exception is thrown:
“No application is associated with the specified file for this operation”
what should i do?
The following code snippet should work, but it does have an issue that might be a deal breaker (continue reading for an explanation):
The only problem with the code above is that it will show the print dialog. I was unable to find a way to supress it, and it seems to be an issue (or feature) specific to printing HTML files.
There’s an ugly workaround if you can tolerate having the print dialog appearing for a second or so, and that is to simulate sending the “enter” key to the print dialog through code. The easiest way to do that is to use the
System.Windows.Forms.SendKeysclass, specifically theSendWaitmethod.So the revised code snippet will look like the following:
The call to the
Sleepis necessary to ensure that the print dialog is fully loaded and ready to receive user input before sending the key press.HTH