0

I have to print html file when user clicks on print button and it is working fine (prompts print dialog) when I set default browser as IE. If I change default browser to chrome or firefox other than IE, the code does not prompting print dialog instead it just opens html file in the browser. Can you please let me know what configuration I have missed in the below code?

            string TempFile = @"D:\test.html";    

            ProcessStartInfo Params = new ProcessStartInfo();
            Params.FileName = "iexplore.exe";
            Params.Arguments = TempFile;
            Params.UseShellExecute = false;
            Params.Verb = "print";
            Params.WindowStyle = ProcessWindowStyle.Hidden;
            Params.CreateNoWindow = true;
            Process.Start(Params);
Prabhakaran Parthipan
  • 4,193
  • 2
  • 18
  • 27
  • Have you tried to [print this file using `WebBrowser` control](https://stackoverflow.com/questions/3934508/printing-a-formatted-html-page-in-c-sharp)? You're trying to launch a browser from command line and it doesn't work as I expect different browsers to have different command line arguments. Technically, you can set different arguments based on `name` (e.g. `if(name=="chrome.exe"){`), but this sounds cumbersome. – default locale May 22 '19 at 03:46
  • I am trying without using WebBrowser control. Actually I don't want to launch a browser instead wants to open print dialog. I have tried with following code. `ProcessStartInfo Params = new ProcessStartInfo(); Params.FileName = TempFile; Params.UseShellExecute = true; Params.Verb = "Print"; Process.Start(Params);` However this code works only if default browser set to IE. – Prabhakaran Parthipan May 22 '19 at 05:41
  • 1
    Is it an option to just always print the file through IE, even with Chrome as a default browser? In this case, you don't even need to go through registry, just set `name = "iexplore.exe"`. This obviously won't work if IE is not installed, though. – default locale May 22 '19 at 05:45
  • No its not IE specific. System should print the page when user set any default browser.If you see the code in my previous comment I have not specified the browser details. – Prabhakaran Parthipan May 22 '19 at 05:51

1 Answers1

3

Finally I got the solution for this issue. The below code works like a charm!!

using (Process exeProcess = new Process())
{
    string TempFile = @"D:\test.html";
    exeProcess.StartInfo.FileName = "rundll32";
    exeProcess.StartInfo.Arguments = @"system32\mshtml.dll,PrintHTML """ + TempFile + @"""";
    exeProcess.StartInfo.UseShellExecute = true;
    exeProcess.Start();
}
Prabhakaran Parthipan
  • 4,193
  • 2
  • 18
  • 27