11

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?

9
  • Is there any sort of error message when it crashes? Commented Aug 26, 2011 at 19:52
  • "{application name} has stopped working. Windows is checking for a solution to the problem" Commented Aug 26, 2011 at 19:55
  • @John, that was a last ditch effort to make the app actually run. I dunno, i've been reading tutorials and staring at my old ASP.NET book and mucking around. Forgot to remove that before I posted my example code. Commented Aug 26, 2011 at 19:56
  • 1
    Anything in the Event Log on the error? Commented Aug 26, 2011 at 19:57
  • Are you using ASP NET Development Server? Commented Aug 26, 2011 at 19:58

5 Answers 5

12

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);
Sign up to request clarification or add additional context in comments.

7 Comments

He already did that, that's what his app.StartInfo.FileName = @"..."; line is.
Worked! This works. However Why does it have to be invoked this way? what is the real difference (just so I am actually learning something and not just copy-paste your answer)
It may have been because you defined your Process variable outside Page_Load, or because you forgot the CWD.
@SpikeX, when I implemented this solution I moved the declaration for psi outside of the Page_Load, however I did forget the Working Directory..
well defining the WorkingDirectory solved my problem and the app now starts without any errors.
|
3

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.

1 Comment

I will be converting the app to a console app eventually. I am completely new to ASP.NET and how it interacts with it's code behind. I am currently knee deep into a few ASP.NET books, learning slowly.
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.

1 Comment

Can you grant access for a folder outside of .NET applications?
0

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.

2 Comments

The app i'm trying to run has very few dependencies. It's a small HMI application I wrote to read values from a Serial Device and display them in a form. Now I want to open the app from a c#/ASP.NET webpage, read the values from the serial device connected to the server, store the data on a db or in a flat file and allow the user to display it now or at a later date.
For that, I recommends you to use a Windows Service to start up the program: this way the HMI can store the data in the database and later, in any application, you can show the data sorted by inserted datetime or something like that.
-1

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");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.