Tutorial :ASP.NET running an EXE File


I´m trying to run an old .NET application from an ASP.NET website. After reading the web and Stackoverflow (for similar problem) I come to the following code. The Problem is that I get always an error code (I am using administrator account just to testing purposes). If I run the exe manually it works ok.

private void Execute(string sPath)  {      System.Diagnostics.Process proc = new System.Diagnostics.Process();      proc.StartInfo.UserName = "administrador";      string pass = ".............";        System.Security.SecureString secret = new System.Security.SecureString();      foreach (char c in pass) secret.AppendChar(c);        proc.StartInfo.Password = secret;      proc.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["WORKINGDIRECTORY"].ToString();      proc.StartInfo.RedirectStandardOutput = true;      proc.StartInfo.RedirectStandardError = true;      proc.StartInfo.UseShellExecute = false;      proc.StartInfo.FileName = sPath;      proc.Start();      proc.WaitForExit();      string result = proc.StandardOutput.ReadToEnd();      Response.Write(result + " - "  + proc.ExitCode);      proc.Close();  }  


The exitcode I get is: -1066598274 Result variable is empty. No exception is thrown I am using Windows 2008 with IIS 7.0

Thanks in advance,



You may need to set the proc.StartInfo.LoadUserProfile property to true so the administrator's user profile stuff is loaded into the registry (AFAIK this does not happen by default).

Also, it might be educational to run a 'hello world' program to see if the problem is with actaully creating the process or if the process itself is having problems running in the context it's given.

Finally, as a step in trying to narrow down where the problem might be, you might want to run the ASP.NET process itself with admin or system credentials to see if something in the permissions of the account the ASP.NET instance is running under is part of the problem (but please do this only for troubleshooting).


Don't do this. This is just plain dirty and should not be done from ASP.NET

  1. Write a windows service
  2. Store the request in a queue
  3. The service should poll the queue and process. If needed run the exe. It is suggested that the service stays in a different server.

Don't do this. This is very bad and not scalable and bad for the web server





    protected void btnSubmit_Click(object sender, System.EventArgs e)      {          if (Page.IsValid)          {              litMessage.Visible = true;                System.Diagnostics.Process oProcess = null;                try              {                  string strRootRelativePathName = "~/Application.exe";                    string strPathName =                      Server.MapPath(strRootRelativePathName);                    if (System.IO.File.Exists(strPathName) == false)                  {                      litMessage.Text = "Error: File Not Found!";                  }                  else                  {                      oProcess =                          new System.Diagnostics.Process();                        oProcess.StartInfo.Arguments = "args";                        oProcess.StartInfo.FileName = strPathName;                        oProcess.Start();                        oProcess.WaitForExit();                        System.Threading.Thread.Sleep(20000);                        litMessage.Text = "Application Executed Successfully...";                  }              }              catch (System.Exception ex)              {                  litMessage.Text =                      string.Format("Error: {0}", ex.Message);              }              finally              {                  if (oProcess != null)                  {                      oProcess.Close();                      oProcess.Dispose();                      oProcess = null;                  }              }          }      }  


If you use

proc.StartInfo.RedirectStandardOutput = true;  

then you have to read the stream as the process executes, instead of before the call to


Same goes for the standard error stream. See the MSDN docs for more detail.


You need to reorder the output reading at the end.

It expects you to read before the waitforexit() call, so you should have:

  proc.Start();  string result = proc.StandardOutput.ReadToEnd();  Response.Write(result + " - "  + proc.ExitCode);  proc.WaitForExit();  proc.Close();  


If the application you're trying to run is really a .NET application as you say, you may not need to run it in a separate process at all. Instead, you can take advantage of the fact that .NET executables are also assemblies. I don't think Visual Studio will let you reference assemblies that end in .exe, but the command-line compiler will.

I would try using the command-line compiler to create a wrapper assembly that simply references the executable assembly, and directly calls its Main() method, passing in a string array of any command-line parameters you would normally specify. The exit code, if any, will be an integer return value from the Main method. Then you can simply call your wrapper assembly from your ASP.NET app.

Depending on what the executable does, and how much it interacts with the console, this approach may not work at all. But if it does work for your case, it should perform much better than spinning up a separate process.


What i do is to have the executable called by a ms sql job. The executable would be run as SQL server agent service account.

  1. Create a new sql server job
  2. Give it a name in the job property's general page
  3. In the steps page, create a new step of type Operating system (CmdExec)
  4. Speeify the command and click ok to save the job parameters

The new job can be called using EXEC msdb.dbo.sp_start_job @jobname, where @jobname is the variable carrying the name of the job you want to start.

Note that when this job starts, the UI of the exe will be hidden and will not be displayed; but you can find it in your task manager.

I have employed this method in several applications especially time consuming operations that cannot be done on the web page.


Use below code:

    ProcessStartInfo info = new ProcessStartInfo("D:\\My\\notepad.exe");      info.UseShellExecute = false;      info.RedirectStandardInput = true;      info.RedirectStandardError = true;      info.RedirectStandardOutput = true;      //info.UserName = dialog.User;         info.UserName = "xyz";      string pass = "xyz";      System.Security.SecureString secret = new System.Security.SecureString();      foreach (char c in pass)          secret.AppendChar(c);      info.Password = secret;      using (Process install = Process.Start(info))      {          string output = install.StandardOutput.ReadToEnd();          install.WaitForExit();          // Do something with you output data                    Console.WriteLine(output);      }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »