Launch a program from ASP.NET C#

I have a program (I created) and I want to start it on the server when the webpage loads.

Here is the code I have

public partial class _Default : System.Web.UI.Page
{
    Process app = new Process();
    protected void Page_Load(object sender, EventArgs e)
    {
        app.StartInfo.FileName = @"D:/Path to /My/Program to be run.exe";
        app.Start();
    }
}

Right now the application is ‘run’ however it crashes instantly.
If I just run the application (by double clicking the exe) it runs and everything is fine.

anyone see if i’m missing something here?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could use ProcessStartInfo.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"D:/Path to /My/Program to be run.exe";
psi.WorkingDirectory = IO.Path.GetDirectoryName(psi.FileName);
Diagnostics.Process.Start(psi);

Method 2

It sounds like the application you’re trying to run has a user interface. If you’re intention is to run this on the server using the ASP.NET application pool account, you will have fewer problems if you design the application as a console app, and guard all access to external resources, like your HMI device, with logged exceptions.

Method 3

This is a security issue. Running any exe from outside the bin folder poses a security threat. You have to copy the exe you are trying to run in the bin folder.

Method 4

It depends what you’re trying to run. Maybe when you run it from your C# app something’s missing. You also might not have the correct permissions to run the app from C#. That’s all I can really say without knowing what’s trying to be run.

Method 5

Have you tried something like this in Javascript :-

var shell = new ActiveXObject("Shell.Application");
var appExe =  @"D:/Path to /My/Program to be run.exe";
shell.ShellExecute(appExe , "", "", "open", "1");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x