Open a html file using default web browser

前端 未结 4 874
执笔经年
执笔经年 2021-01-04 00:57

I\'m using this to get the path and executable of default web browser:

public static string DefaultWebBrowser
        {
            get
            {

               


        
相关标签:
4条回答
  • 2021-01-04 01:02

    Im using a code, where i look up for exe files first. For example, if exsists chrome.exe (in its default path) else if exists firefox.exe or launcher.exe (for opera) etc... if doesnt any exists, try to run iexplore.exe with pathToHtmlFile parameter. This is my solution, where i use external config, where a set my browser, doesnt matter what is set default in OS.

    0 讨论(0)
  • 2021-01-04 01:11

    You can replace all that code with

    System.Diagnostics.Process.Start(pathToHtmlFile);
    

    This will automatically start your default browser, or rather look up the default handler for .htm or .html files and use that.

    Now with Firefox set as default this can sometimes cause weird exceptions (I think if Firefox is starting for first time), so you might want to do a try/catch on it to handle that.

    0 讨论(0)
  • 2021-01-04 01:14

    For .Net Core you need to call (suggested in .Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform")

     var proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile); 
    

    When I tried Process.Start(pathToHtmlFile);, I've got System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform.

    0 讨论(0)
  • 2021-01-04 01:18

    For those who don't have html default association to a browser, use

    System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))

    0 讨论(0)
提交回复
热议问题