1
Reply

Help with running external process from button click in Web form

Larry Walter

Larry Walter

Sep 17 2010 8:44 PM
1.9k

We need assistance with a portion of a major web application project scheduled to begin user testing within 10 days.  The troublesome part of the code calls an external application when a button is clicked in Web page.  When run from a command prompt or bat file, the external application, App.exe, runs perfectly producing the desired result.  We created a test page and a small console application, Con.exe, to test the button code with a known quantity.  The test application runs perfectly when the button in the Web page is clicked.  However when we change the code to run App.exe instead of Con.exe, the App.exe code will not execute.  We even tried creating a bat file to execute App.exe.  Like the exe, the bat file executes from the command prompt or when double-clicked, but fails to run when called as a process from the button click event.  Can anyone assist us?  Here is the C# code: (.Net 3.5 sp1)

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Diagnostics;

 

namespace WebButtonTest

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            

 

        }

 

        protected void Timer1_Tick(object sender, EventArgs e)

        {

            int checkNumber = 0;

            if (Session["checkNumber"] != null)

                int.TryParse(Session["checkNumber"].ToString(), out checkNumber);

            else

                Session["checkNumber"] = 0;

            Session["checkNumber"] = ((int)Session["checkNumber"]) + 1;

 

            if (Session["process"] == null || !(Session["process"] is Process))

            {

                Session["process"] = null;

                lblStatus.Text = "Process not running - Possibly an error?!?";

                Timer1.Enabled = false;

            }

            else

            {

                Process p = (Process)Session["process"];

                if (!p.HasExited)

                    lblStatus.Text = "Process Running ... Last Check: " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "; Check Number: " + checkNumber.ToString();

                else

                {

                    Session["process"] = null;

                    lblStatus.Text = DateTime.Now.ToString() + ": Process complete";

                    Timer1.Enabled = false;

                }

            }

 

           

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            System.Diagnostics.Process p = new System.Diagnostics.Process();

 

/*********WHEN THE PROJECT IS BUILT WITH THE FOLLOWING 3 LINES THE CODE WILL RUN*********/

            p.StartInfo.Arguments = "WEBTest";

            p.StartInfo.WorkingDirectory = @"c:\";

            p.StartInfo.FileName = "Con.exe";

/*********END OF SUCCESSFUL CODE*********/

 

/********WHEN THE PROJECT IS BUILT WITH THE FOLLOWING 3 LINES THE CODE WILL NOT RUN*******/

            p.StartInfo.Arguments = "9995";

            p.StartInfo.WorkingDirectory = @"c:\";

            p.StartInfo.FileName = "App.exe";

/*********END OF UNSUCCESSFUL CODE*********/

 

 

            p.Start();

            p.WaitForExit();

            Session["process"] = p;

            Timer1.Enabled = true;

 

        }

    }

}
 


Answers (1)