-1

Using C#, WPF, .NET Framework. Any browser.

It is easy to open the default browser with an HTTP URL using Process.Start.

How would one open a PDF file in the default browser using Process.Start?

As per Microsoft documentation, I have tried Process.Start("file:///"+filename) where filename is the explorer filename with "\" replaced with "/". For example, Process.Start("file:///"+"C:/My Folder/My PDF file.pdf").

In this case, the default PDF reader is always opened, not the browser. Is this because there is a PDF reader installed? Would it use the browser correctly if there was not a PDF reader installed?

Even with a PDF reader installed, I want to force the file to be opened in the default browser.

How might one do that?

Braiam
  • 1
  • 11
  • 47
  • 78
Jeronymite
  • 51
  • 1
  • 2
  • 11
  • 1
    Have you tried just using `Process.Start()` on the file itself and seeing if the OS will handle which default app to use to view that file? – Jaskier Sep 02 '21 at 01:51
  • Yes. It always uses the installed PDF Reader. – Jeronymite Sep 02 '21 at 02:08
  • 4
    Is that because the installed PDF Reader is set to the default PDF viewer for the executing user? Have you tried changing the default PDF viewer on the computer to a different application and seeing if that gets fired off instead? From the context, it sounds like you're trying to ignore user-set OS settings. – Jaskier Sep 02 '21 at 02:20
  • Does this answer your question? [How to find default web browser using C#?](https://stackoverflow.com/q/13621467) – Spencer Bench Sep 02 '21 at 04:09
  • The default PDF viewer is the installed PDF Reader. However, as mentioned, I want to be able to force the PDF file to be opened in the default browser.The link provided above is useful, thanks. Nevertheless, I would appreciate a solution in context here as to how to integrate knowing the default browser and the Process.Start to make it display the file. Thanks. – Jeronymite Sep 02 '21 at 06:15
  • 2
    "I want to be able to force the PDF file to be opened in the default browser" again why? What do you win with forcing your preferences rather than the users? This behavior hurts compatibility of your software and will break whenever MS moves the cheese around. – Braiam Oct 08 '21 at 13:55

2 Answers2

2

Get the installation directory of the default browser, then use Process.Start launch the browser and open the pdf file. E.g:

Process.Start("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", "file:///PdfFilePath");
Anders Chen
  • 83
  • 3
  • 7
-2
Process proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "file name here.pdf";
proc.Start();
Shaido
  • 27,497
  • 23
  • 70
  • 73
Zekira
  • 1
  • 1
    Welcome to StackOverflow! Posting Code-only answer isn't very helpful- context helps! Please add some explanation to what your code is doing and why it would solve OP's issue! :) – Jaskier Sep 02 '21 at 02:21
  • I tried this. It uses the default PDF viewer, not a browser. – Jeronymite Sep 02 '21 at 06:16