Run exe from webpage

We have an internal page that I want to use to run an executable that updates some files on the server. In other words, instead of logging into the server every time I need to manually launch this executable, I would like to launch it from the browser. The executable is self-contained on the server and does not interact with the user.

Here is my code:

    try
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = Server.MapPath(@"\iPhoneXMLCreator.exe");
        p.StartInfo.WorkingDirectory = Server.MapPath(@"\");
        p.StartInfo.RedirectStandardOutput = false;

        p.Start();
        p.WaitForExit();
        lblResult.Text = "Success!";
    }
    catch (Exception ex)
    {
        lblResult.Text = "Oops, there was a problem.<br><Br>" + ex.Message;
    }

      

When I run it, the process appears in the task manager, but then exits after a few seconds without updating the files it should be using. There are no arguments to pass, just a simple executable. Any ideas?

+2


source to share


6 answers


I would start by checking if the account that runs the web application has the appropriate permissions.



+5


source


This is most likely a permissions issue. Since it is an Asp.Net executable program executing this program, you need to make sure that the user account used during Asp.Net runtime has access to this executable and modify any resources (files, databases, etc.) That modified the executable file.

You can do this through impersonation or by granting rights to the appropriate accounts. The correct approach is to use impersonation.



http://msdn.microsoft.com/en-us/library/xh507fc5.aspx

+1


source


Does the executable get executed and process the XML when you run it manually on a server registered as itself?

Then it might be a simple permissions issue, because if you're not impersonating the truth ... it might be trying to run the exe under the ASPNET machine account, which most likely doesn't have permissions on the folder that the XML is in. a thought based on the information you provided.

+1


source


2 things you could do:

  • Start Process Monitor while you are trying to run the exe. I've used it many times to help find security configuration issues (especially on web servers). It will log every io and registry access and, more importantly, indicate success or failure. Get it here: http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx It doesn't require installation. Great tool!

  • Redirect stdout to exe console. This will allow you to log any error message it tries to write to the console. Here's how to do it:


try    
{        
  System.Diagnostics.Process p = new System.Diagnostics.Process();        
  p.StartInfo.UseShellExecute = false;        
  p.StartInfo.FileName = Server.MapPath(@"\iPhoneXMLCreator.exe");        
  p.StartInfo.WorkingDirectory = Server.MapPath(@"\");        

  // redirect stdout
  p.StartInfo.RedirectStandardOutput = true;       
  var ConsoleOutput = new StringBuilder();
  p.OutputDataReceived += (s, e) => ConsoleOutput.AppendLine(e.Data);  

  p.Start();        
  p.BeginOutputReadLine(); // if I remember correctly, you have to call Start() first or you get an exception
  p.WaitForExit();        
  string output = ConsoleOutput.ToString();
  lblResult.Text = "Success!";    
}    
catch (Exception ex)    
{        
  lblResult.Text = "Oops, there was a problem.

" + ex.Message; }


+1


source


Instead of playing with website permissions for Exe, one workaround that uses a level of indirection and places a buffer between your website and Exe is to simply set the flag to a text file on the web server when the page representing Exe ...

Configure a scheduled task on the server to check the value of this flag every X hours or minutes, or when and if the flag is displayed, run the executable. Reset flag / file upon completion. This opens up the possibility to check the flag through the web service or other mechanisms, so that the target Exe doesn't even have to be on the same web server machine.

This is only possible if the exe does not need to be started immediately when the pages are damaged.

0


source


Okay, got it. This is a data access issue. The .config file for the .exe had an invalid database connection string. Why it will work on login I'm not sure, but it works now.

0


source







All Articles