简体   繁体   中英

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, rather than logging in to the server every time I need to manually run this executable, I would like to run 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 shows up in Task Manager, but then exits within a few seconds without updating the files it is supposed to. There are no arguments to be passed, just a simple executable. Any ideas?

我首先检查运行Web应用程序的帐户是否具有适当的权限。

Most likely this is a permissions issue. Since it's the Asp.Net runtime that is executing this program, you need to ensure that the user account that the Asp.Net runtime uses has access to this executable, and to modify any resources (files, databases, etc) that get modified by the executable.

You can do this via impersonation, or by granting rights tot he appropriate accounts. The proper approach is to use impersonation.

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

Does the executable file run and process the XML when you run it manually on the server logged in as yourself?

Then it may be a simple permissions issue, since unless you are impersonating ...it's probably trying to run the exe under the ASPNET machine account, which most likely doesn't have rights to the folder the XML is in. Just a thought based on the info you provided.

2 things that you could do:

  1. Run Process Monitor while you attempt to run the exe. I've used it many times to help to find security config problems (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 requires no setup. Great tool!

  2. Redirect stdout on your console exe. This will allow you to capture any error message that it is attempting 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; }

Rather than playing with website permissions for the Exe, one workaround that uses a level of indirection and puts a buffer between your web site and the Exe is to simply set a flag value into a text file on the web server when the Page representing the Exe is hit.

Set up a scheduled job on the server to check for that flag value every X hours, or minutes, or whenever and if the flag is seen, run the executable. Reset the flag/file when done. This opens up the possibility to check the flag via a webservice or other mechanisms, such that the target Exe doesn't even need to be on the same web server machine.

This is only viable if the exe does not need to run immediately when the page is hit.

Ok, figured it out. It was a data access issue. The .config file for the .exe had an invalid database connection string. Why it would work when logged in, I'm not sure, but it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM