C# System.Diagnostics.Process verb issue
I have a Windows Application written in C# VS 2008. The purpose of this application is to convert any file to PDF files. I have found code that works on converting the files however there i开发者_StackOverflow中文版s a small issue that I am coming across.
First here is the code:
private void PrintToAdobePDF(string strInputFilePath)
{
ProcessStartInfo pProcInfo = new ProcessStartInfo();
bool blResult;
blResult = SetDefaultPrinter(D2P_Adobe_Printer);
if (blResult)
{
pProcInfo.FileName = strInputFilePath;
pProcInfo.Verb = "Print";
pProcInfo.CreateNoWindow = true;
pProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process pProc = Process.Start(pProcInfo);
pProc.WaitForExit(1000);
pProc.CloseMainWindow();
pProc.Close();
}
The issue I am having is that when the Process.Start() method is invoke it is running with TWO verbs instead of the one verb I specified ("Print"). It is running "Open" and "PrintTo" which is making the application that the original file is derived from open up and hang the application (i.e. jpg opens the Windows Fax and Picture Viewer). My question is how do I just use the "Print" verb within the Process.Start() method?
Thank you in advance
Have you tried researching if it's possible to execute Adobe Reader with a command line parameter that accomplishes the same thing? Relying on the shell is iffy sometimes.
What are sending in for strInputFilePath? The documentation says to only send the filename so if you are sending the whole path that could be causing the issue.
No, sending the filename without the extension will fail.
Ultimately using System.Diagnostics.Process to print any arbitrary file is going to be unpredictable at best. It's all up to how your operating system handles each type of file, and whether or not your registry is properly configured to handle that file.
I'd guess that printing .doc files in this manner probably works OK, while other file types may not work so well.
In my opinion, you should find some constraint about the kinds of files you'll allow to "automagically" print, and build working solutions per type of file. Otherwise, you'll find a lot of unpredictable behavior.
精彩评论