Click here to Skip to main content
15,173,818 members
Articles / Web Development
Technical Blog
Posted 28 Sep 2010


22 bookmarked

How to execute a Local File using HTML Application?

Rate me:
Please Sign up or sign in to vote.
3.82/5 (10 votes)
28 Sep 2010CPOL2 min read
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);

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:

    <title>Application Executer</title>
	    APPLICATIONNAME="Application Executer" 
    <script type="text/javascript" language="javascript">
        function RunFile() {
		WshShell = new ActiveXObject("WScript.Shell");
		WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
	<input type="button" value="Run Notepad" onclick="RunFile();"/>

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.

This article was originally posted at


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


About the Author

Kunal Chowdhury «IN»
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog ( and CodeProject.

Books authored:

Connect with Kunal on:

Comments and Discussions

QuestionWorked nice for Fire fox and clicking a batch file with Python script inside.. Thank U. Pin
Member 1499380721-Jul-21 20:13
MemberMember 1499380721-Jul-21 20:13 
SuggestionThis will work if Pin
Kamyar Mortazaee11-Apr-20 13:48
MemberKamyar Mortazaee11-Apr-20 13:48 
QuestionWorks only in Internet Explorer Pin
Engr. S.M. Inuwa30-Dec-17 1:18
MemberEngr. S.M. Inuwa30-Dec-17 1:18 
QuestionThe code doesn't work Pin
Eddy Quicksall28-Oct-17 15:45
MemberEddy Quicksall28-Oct-17 15:45 
AnswerRe: The code doesn't work Pin
jsc423-Nov-17 5:46
professionaljsc423-Nov-17 5:46 
QuestionWorks with Notepad - Why not Snippingtool Pin
Member 1326358616-Jun-17 6:56
MemberMember 1326358616-Jun-17 6:56 
QuestionNot working in Chrome and/or Firefox Pin
Member 1023303720-Jul-15 2:19
MemberMember 1023303720-Jul-15 2:19 
GeneralNot starting notepad Pin
Member 976283912-Jun-14 22:26
MemberMember 976283912-Jun-14 22:26 
GeneralRe: Not starting notepad Pin
Member 1326358616-Jun-17 8:00
MemberMember 1326358616-Jun-17 8:00 
GeneralFunction works Pin
jagman825-Feb-14 9:18
Memberjagman825-Feb-14 9:18 
QuestionNot working after hosting on a server Pin
Rakesh690631-Oct-13 8:05
MemberRakesh690631-Oct-13 8:05 
QuestionRun() function arguments Pin
Member 215809416-Sep-13 0:47
MemberMember 215809416-Sep-13 0:47 
GeneralError Pin
Member 442576719-Oct-10 12:07
MemberMember 442576719-Oct-10 12:07 
AnswerRe: Error Pin
Kunal Chowdhury «IN»20-Oct-10 8:41
professionalKunal Chowdhury «IN»20-Oct-10 8:41 
GeneralRe: Error Pin
1ni3-Mar-13 22:14
Member1ni3-Mar-13 22:14 
GeneralRe: Error Pin
Kunal Chowdhury «IN»20-Oct-10 8:44
professionalKunal Chowdhury «IN»20-Oct-10 8:44 
GeneralDoesnt work Pin
Member 442576719-Oct-10 11:58
MemberMember 442576719-Oct-10 11:58 
AnswerRe: Doesnt work Pin
Kunal Chowdhury «IN»20-Oct-10 8:38
professionalKunal Chowdhury «IN»20-Oct-10 8:38 
GeneralMy vote of 5 Pin
Richard Waddell7-Oct-10 16:23
MemberRichard Waddell7-Oct-10 16:23 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»20-Oct-10 8:37
professionalKunal Chowdhury «IN»20-Oct-10 8:37 
GeneralMy vote of 4 Pin
JackDanyelZ0028-Sep-10 10:41
MemberJackDanyelZ0028-Sep-10 10:41 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»28-Sep-10 16:31
professionalKunal Chowdhury «IN»28-Sep-10 16:31 
GeneralRe: My vote of 4 Pin
rahul.kulshreshtha22-May-13 18:31
Memberrahul.kulshreshtha22-May-13 18:31 

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

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