Launch-local-computer-exe-from-web-page

Launch local computer exe from web page

Launch local computer exe from web page

Hello folks, in my last post I have explained regarding Register custom URL Protocol using C# – Windows Application. If you haven’t read it I strongly recommend to read and understand first cause we will be using URL Protocol concept in our current post. Today we will discuss regarding Launch local computer exe from web page using parameters passed from web page. Till now we have discussed regarding 4 ways to Convert JSON to DataTable in C# – asp.netScheduler for multiple instances in C#Difference between Abstraction and Encapsulation in C# and Difference between Throw and Throw ex in C#.

Step-1: Register URL protocol  in Registry by using below code snippet

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Custom_URL_Protocol
{
    class Program
    {
        static void Main(string[] args)
        {
            RegisterURLProtocol("TechoThirstyParam", @"PathforYourEXE");
        }

        // using Microsoft.Win32; do not forget!
        /// </summary>
        /// <param name="protocolName">Name of the protocol (e.g. "TechoThirstyParam"")</param>
        /// <param name="applicationPath">Complete file system path to the EXE file, which processes the URL being called.</param>
        public static void RegisterURLProtocol(string protocolName, string applicationPath)
        {
            try
            {
                // Create new key for desired URL protocol

                var KeyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true);
                RegistryKey key = KeyTest.CreateSubKey(protocolName);
                key.SetValue("URL Protocol", protocolName);
                key.CreateSubKey(@"shell\open\command").SetValue("", "\"" + applicationPath + "\" \"%1\"");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
    }
}

 

In Registry we got registered our “TechoThirstyParam” registered as below screenshot. We got addition “%1” to pass parameters in exe from web page.

 

Launch-local-computer-exe-from-web-page-with-parameters-Registry-entry

 

Step-2: Create sample exe which could capture parameters using below code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppWithParameters
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var item in args)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

In this code we will capture parameters in “args” array and we will try to iterate parameters which we will pass through our HTML page.

Step-3: Create HTML page to run our “TechoThirstyParam” URL protocol as below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Launch Technothirsty Custom URL </title>
    <script type="text/javascript">
        function LaunchURLScript() {
            var url = "TechoThirstyParam: param1=test param2=test1";
            window.open(url);
            self.focus();
        }
    </script>
</head>
<body style="background-color:#D7D7D7">
    <input type="submit" name="Launch" id="Launch" value="Launch Technothirsty Custom URL" onclick="LaunchURLScript()">
</body>
</html>

As you can see we have called our URL protocol by using different parameters named with param1 and param2 using “TechoThirstyParam: param1=test param2=test1”;

 

OUTPUT:

Launch-local-computer-exe-from-web-page-output

So Folks, today we have seen how we can Launch local computer exe from web page with parameters.

One comment

Leave a Reply