tags:

views:

34

answers:

3

This question could be a can of worms, but I've always wondered if it were possible to invoke an executable application from inside a web application on the web server?

I can easily set this up as a scheduled task to run and check every few minutes, but it would be nice to be able to invoke the application on demand from the a web page.

The site is an ASP.NET 2.0 website using C# and the application is an executable console app written in C#.

+1  A: 

Process.Start

Darin Dimitrov
+1  A: 

You need to start a new process. This is an example how to do it.

        Process         proc                = new Process();
        StringBuilder   sb                  = new StringBuilder();
        string[]        aTarget             = target.Split(PATH_SEPERATOR); 
        string          errorMessage;
        string          outputMessage;

        foreach (string parm in parameters)
        {
            sb.Append(parm + " ");
        }

        proc.StartInfo.FileName                 = target;
        proc.StartInfo.RedirectStandardError    = true;
        proc.StartInfo.RedirectStandardOutput   = true;
        proc.StartInfo.UseShellExecute          = false;
        proc.StartInfo.Arguments                = sb.ToString();

        proc.Start();

        proc.WaitForExit
            (
                (timeout <= 0)
                ? int.MaxValue : (int)TimeSpan.FromMinutes(timeout).TotalMilliseconds  //Per SLaks suggestion.  Thanks!
            );


        errorMessage    = proc.StandardError.ReadToEnd();
        proc.WaitForExit();

        outputMessage   = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();

A link to MSDN:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(VS.71).aspx

You'll also need to check to make sure that the account the Web application is running under has the appropriate permissions to execute the program.

Kevin
`(int)TimeSpan.FromMinutes(timeout).TotalMilliseconds`
SLaks
+1  A: 

I think you could do that using the following code.

Process p= new Process();
p.StartInfo.WorkingDirectory = @"C:\whatever";
p.StartInfo.FileName = @"C:\some.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();

where the Process type is from the namespace System.Diagnostics.Process

Krishna