Click here to Skip to main content
Click here to Skip to main content

How to execute a Local File using HTML Application?

, 28 Sep 2010
Rate this:
Please Sign up or sign in to vote.
How to execute a Local File using HTML Application?

Did you ever want to run an application from your local resource inside your HTML page? If so, you will face some issues. It will never run any application from local resource directly. It may ask you to download the file before running it. This is due to browser security issue as the code runs inside the browser sandbox.

So, what to do for this? In this post, I will describe the steps to do this. Read the complete post in order to learn the same.

As I mentioned above, you can't run an application from a browser window due to security reasons. To execute file, you need to use .HTA applications (which is an HTML application). HTA applications run outside the browser window just like a normal application and have full trust support.

If you are new to HTML application, read more about it here.

Let’s jump to create one HTML application to launch Notepad for you (without any security warnings). To do this, design your HTML page with a button, which will execute the Notepad once clicked. You can set some application specific properties using the <HTA:APPLICATION /> tag. This step is optional. Add the following JavaScript code inside your <head /> tag:

    <script type="text/javascript" language="javascript">
        function RunFile() {
		WshShell = new ActiveXObject("WScript.Shell");
		WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
        }
    </script>

Here, you can see that first I am creating the instance of the “WScript.ShellActiveXObject and then calling the Run() method of the newly created object with proper parameters.

Now, from the button click event, give a call to the JavaScript method named “RunFile()”. Once you click on the button, it will execute the file mentioned in the Run() method.

If you run this code inside the browser, it will not work. To resolve this, just save the file with a .hta extension. You will see that the file icon has changed to an application icon. If you double click on this file, it will execute and open a Window having the button inside it. Click the button to open the notepad.exe file.

Here is the full code for that:

<html>
<head>
    <title>Application Executer</title>
    <HTA:APPLICATION ID="oMyApp" 
	    APPLICATIONNAME="Application Executer" 
	    BORDER="no"
	    CAPTION="no"
	    SHOWINTASKBAR="yes"
	    SINGLEINSTANCE="yes"
	    SYSMENU="yes"
	    SCROLL="no"
	    WINDOWSTATE="normal">
    <script type="text/javascript" language="javascript">
        function RunFile() {
		WshShell = new ActiveXObject("WScript.Shell");
		WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
        }
    </script>
</head>
<body>
	<input type="button" value="Run Notepad" onclick="RunFile();"/>
</body>
</html>

So, what is the purpose of it? You may need it for various reasons, but that will depend on your requirement. Recently, I needed it to do one ongoing R&D and this concept helped me a lot. Hence, I thought of sharing this with you so that you can also learn and use it as per your requirement.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralNot starting notepad [modified] PinmemberMember 976283912-Jun-14 21:26 
GeneralFunction works Pinmemberjagman825-Feb-14 8:18 
QuestionNot working after hosting on a server PinmemberRobot With Defects31-Oct-13 7:05 
QuestionRun() function arguments PinmemberMember 215809415-Sep-13 23:47 
GeneralError PinmemberMember 442576719-Oct-10 11:07 
AnswerRe: Error PinmentorKunalChowdhury20-Oct-10 7:41 
GeneralRe: Error Pinmember1ni3-Mar-13 21:14 
GeneralRe: Error PinmentorKunalChowdhury20-Oct-10 7:44 
GeneralDoesnt work PinmemberMember 442576719-Oct-10 10:58 
AnswerRe: Doesnt work PinmentorKunalChowdhury20-Oct-10 7:38 
GeneralMy vote of 5 PinmemberRichard Waddell7-Oct-10 15:23 
GeneralRe: My vote of 5 PinmentorKunalChowdhury20-Oct-10 7:37 
GeneralMy vote of 4 PinmemberJackDanyelZ0028-Sep-10 9:41 
GeneralRe: My vote of 4 PinmentorKunalChowdhury28-Sep-10 15:31 
GeneralRe: My vote of 4 Pinmemberrahul.kulshreshtha22-May-13 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 28 Sep 2010
Article Copyright 2010 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid