Search code examples
c#dotnetbrowser

How to allow DotNetBrowser v. 1.21.5.0 to access EXE file stored on local machine


i have a specific questions in regards to using DotNetBrowser library in C# project. I am asking because i have already seen some answers for DotNetBrowser here and i figured someone might have an answer for me.

Here is the problem: The organisation i am with is currently using DotNetBrowser version 1.21.5.0 to display some web pages inside an application written in C#. However, some our pages have links to fire an 'exe' files stored on local machines. For some reason this is failing in this version of DotNetBrowser.

Is there a setting that i could set to instruct the browser to allow firing the local exe. In chrome this works fine. Strangely there is little documentation found on this version, or the links provided no longer work. All working solutions point to newer version which is useless to me. I am specifically referring to version 1.21.5.0.

Any advice would is appreciated.


Solution

  • For this purpose, you need to use a custom load handler or intercept the required URL in the Browser.LoadHandler and pass it to the application via Process.Start() method.

    The following article describes how such applications are registered in Windows system: https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx

    The below example demonstrates handling mailto protocol and launching the default e-mail program registered in Windows:

    using System.Diagnostics;
    using System.Windows;
    using DotNetBrowser;
    using DotNetBrowser.WPF;
    
    namespace WpfApplication8
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                WPFBrowserView browserView = new WPFBrowserView();
                browserView.Browser.LoadHandler = new CustomLoadHandler();
                mainGrid.Children.Add(browserView);
                browserView.Browser.LoadHTML("<a href='mailto:[email protected]'>[email protected]</a>");
            }
    
            private class CustomLoadHandler : DefaultLoadHandler
            {
                public override bool OnLoad(LoadParams loadParams)
                {
                    if (loadParams.Url.StartsWith("mailto:"))
                    {
                        Process.Start(loadParams.Url);
                        return true;
                    }
    
                    return base.OnLoad(loadParams);
                }
            }
        }
    

    Note: This sample works only if any e-mail program is already registered in Windows. Otherwise, you should provide the full path to the e-mail program and add the arguments from the URL (like e-mail address, subject, etc.):

    Process.Start("Full path to the program", "Arguments (e-mail address, subject, etc.)")

    The approach is identical for any other program you would like to launch.