0

I need to show a PDF file in a browser, by pressing a button in my C# app. So the browser is eternal to my app. On Win 10 I do it like this:

....
var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
    FileName = FindBrowser(),
    Arguments = $"\"file:///{PDF_File}\""
}
process.Start();
....
private static string FindBrowser()
{
GroupCollection groups;
try
{
   // finding the default browser in the registry is a two-step process:
   using var key = Registry.CurrentUser.OpenSubKey(
   @"SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations\http\UserChoice");
   var s = (string) key?.GetValue("ProgId");
   using var command = Registry.ClassesRoot.OpenSubKey($"{s}\\shell\\open\\command");
   var browserCommand = (string) command?.GetValue(null);
   var regexpr = new Regex("^\"([^\"]*)\"");
   groups = regexpr.Match(browserCommand).Groups;
   if (!File.Exists(groups[1].Value))
   {
       throw new FileNotFoundException("NoBrowser");
   }
}

but my customer says this does not work on Win 11 (I have no Win 11) Is there a foolproof way to program this? I use VS 2019 myself to develop and debug. My solution works OK there.

Erik
  • 894
  • 1
  • 8
  • 25
  • Does this answer your question? [c# open file with default application and parameters](https://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters) – Rahatur Jan 04 '23 at 14:37

0 Answers0