Click here to Skip to main content
13,086,019 members (83,252 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 3 Feb 2013

Run .exe inside ASP.NET and catch exception using AppDomain

, 18 Feb 2013
Rate this:
Please Sign up or sign in to vote.
.NET exe file run inside and catch exception from .exe.


I will try to explain how any .NET executable file can be run inside the ASP.NET environment and exceptions from the executable file caught as well.


If you search on line with title like "Execute any executable from" you can find various solution with code example. But when you want to handle any exception thrown from the executable file, you cannot catch that exception.

Common Solution may found  

You can find many code example. one code example like as follows:

    // Create An instance of the Process class responsible for starting the newly process.
    System.Diagnostics.Process process1 = new System.Diagnostics.Process();
    // Set the filename name of the file you want to execute/open
    process1.StartInfo.FileName = @"c:\temp\File1.exe";
    process1.StartInfo.Arguments = "args";

    // Start the process without blocking the current thread
    // you may wait until finish that executable
    //or you can wait for a certain time interval 
    Thread.Sleep(20000);//Assume within 20 seconds it will finish processing. 
catch (Exception ex)

Issue with above code 

Code is working fine. Where the issue is? Well, you can change you .exe file code and throw exception from that executable. Then you see that you cannot catch that exception from current solution.

Why not catch exception? 

When you run any .exe file with the Process object it then create a new independent process and run .exe under that process. For that reason you cannot catch exception. Then what will be solution? 

What will be the solution? 

.NET AppDomain comes with a solution. First you need to create a AppDomain, then .exe file should run inside that AppDomain. AppDomain will not create any independent process, instead it create a isolated environment, this environment will create under current assembly process and any time you can destroy it without any problem.

Is it possible to provide example with code? 

The solution code are as follows

    //Create a new appdoamin for execute my exe in a isolated way.
    AppDomain sandBox = AppDomain.CreateDomain("sandBox");
        //Exe executing with arguments
        sandBox.ExecuteAssembly(".exe file name with path");
        AppDomain.Unload(sandBox);//destry created appdomain and memory is released.
catch (Exception ex)//Any exception that generate from executable can handle

Points of Interest 

I found a limitation on that solution. The limitation is Executable (.exe) must be .NET assembly. No native .exe will work there.


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


About the Author

S. M. Ahasan Habib
Bangladesh Bangladesh
I believe in and stay with living-learning culture. I believe learning is fun. I feel coding is like poetry. I am work with various technologies in my professional career (AngularJS, ASP.NET MVC/WebAPI, Scala-Play Framework, Nodejs, SharePoint, Redshift).

You may also be interested in...


Comments and Discussions

Question.NET Executable Pin
Sarafian18-Feb-13 21:03
memberSarafian18-Feb-13 21:03 
AnswerRe: .NET Executable Pin
S. M. Ahasan Habib18-Feb-13 22:14
memberS. M. Ahasan Habib18-Feb-13 22:14 
GeneralRe: .NET Executable Pin
Sarafian19-Feb-13 21:17
memberSarafian19-Feb-13 21:17 
GeneralRe: .NET Executable Pin
S. M. Ahasan Habib19-Feb-13 22:28
memberS. M. Ahasan Habib19-Feb-13 22:28 
SuggestionState the usage of AppDomain in the title Pin
Edo Tzumer3-Feb-13 20:41
memberEdo Tzumer3-Feb-13 20:41 
GeneralRe: State the usage of AppDomain in the title Pin
S. M. Ahasan Habib18-Feb-13 4:22
memberS. M. Ahasan Habib18-Feb-13 4:22 
GeneralMy vote of 5 Pin
Edo Tzumer3-Feb-13 20:36
memberEdo Tzumer3-Feb-13 20:36 
GeneralRe: My vote of 5 Pin
S. M. Ahasan Habib4-Feb-13 17:46
memberS. M. Ahasan Habib4-Feb-13 17:46 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 18 Feb 2013
Article Copyright 2013 by S. M. Ahasan Habib
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid