How can i run a batch file asp.net

E

emman_54

Hi every one,

I am trying to run a batch file using my asp.net application.

I am using the Process class to run the batch file. When I run my web
application, In the task manager, i could see cmd.exe with ASPNET as a
user. But nothing happens. It can't execute the batch file.

This is the code i am using to run the batch file:

private void Request()

{

string Fichier=Server.MapPath("")+"\\SearchEng\\request.bat";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;


proc.StartInfo.WorkingDirectory=Server.MapPath("")+"\\SearchEng\\";

proc.StartInfo.FileName=Fichier;



try

{

proc.Start();


proc.WaitForExit();

proc.Close();

}


catch(Exception ex)

{

this.lblerror.Text=ex.Message;

}

}



********************************************************************

string Fichier=Server.MapPath("")+\\SearchEng\\request.bat;
Response.Write(Fichier)

It does return the physical path of the file starting from
C:\myApplication\SearchEng\request.bat.

But if don't run the file from the physical path (example : if fichier
= SearchEng\request.bat) it does give me an error : The system cannot
find the file specified.

I did another test: When i manually click on the request.bat file it
does execute and returns me the right answers.

This is what i get on my dos screen when i run the bat file with my
code.

C:\myApplication\SearchEng>set
path="C:\myApplication\SearchEng";C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program
Files\ATI Technologies\ATI Control Panel;C:\Program Files\Microsoft SQL
Server\80\Tools\BINN;C:\bin C:\myApplication\SearchEng>java -jar
"C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps IPSearch
Version 1.22 All rights reserved ¬ 2005 my company Researche in
progress ...... Error Results are saved in the Results.xml

You can see research is in progress. I think the Process end before the
SearchEngine.jar finishes executing.

But when i run it manually i don't no error, Very strange

I tried this proc.WaitForExit(900000) or proc.WaitForExit() but still
ended without success.

The content of my batch file is :
set path="C:\myApplication\SearchEng";%path%
java -jar "C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps

This batch file execute : SearchEngine.jar in dos then it saves the
results in Results.xml

Then i get the results then list them in a treeview.

Have a wonderfull day.
 
J

John Timney \( MVP \)

As a start, when you run a process from asp.net it cant invoke windows
unless you run the asp.net account under a specific domain user profile.
Get rid of your windowstyle option and see if it runs.

You have to get in the mindset of the webserver, no windows, no desktop, few
rights.

--
Regards

John Timney
Microsoft MVP

Hi every one,

I am trying to run a batch file using my asp.net application.

I am using the Process class to run the batch file. When I run my web
application, In the task manager, i could see cmd.exe with ASPNET as a
user. But nothing happens. It can't execute the batch file.

This is the code i am using to run the batch file:

private void Request()

{

string Fichier=Server.MapPath("")+"\\SearchEng\\request.bat";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;


proc.StartInfo.WorkingDirectory=Server.MapPath("")+"\\SearchEng\\";

proc.StartInfo.FileName=Fichier;



try

{

proc.Start();


proc.WaitForExit();

proc.Close();

}


catch(Exception ex)

{

this.lblerror.Text=ex.Message;

}

}



********************************************************************

string Fichier=Server.MapPath("")+\\SearchEng\\request.bat;
Response.Write(Fichier)

It does return the physical path of the file starting from
C:\myApplication\SearchEng\request.bat.

But if don't run the file from the physical path (example : if fichier
= SearchEng\request.bat) it does give me an error : The system cannot
find the file specified.

I did another test: When i manually click on the request.bat file it
does execute and returns me the right answers.

This is what i get on my dos screen when i run the bat file with my
code.

C:\myApplication\SearchEng>set
path="C:\myApplication\SearchEng";C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program
Files\ATI Technologies\ATI Control Panel;C:\Program Files\Microsoft SQL
Server\80\Tools\BINN;C:\bin C:\myApplication\SearchEng>java -jar
"C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps IPSearch
Version 1.22 All rights reserved ¬ 2005 my company Researche in
progress ...... Error Results are saved in the Results.xml

You can see research is in progress. I think the Process end before the
SearchEngine.jar finishes executing.

But when i run it manually i don't no error, Very strange

I tried this proc.WaitForExit(900000) or proc.WaitForExit() but still
ended without success.

The content of my batch file is :
set path="C:\myApplication\SearchEng";%path%
java -jar "C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps

This batch file execute : SearchEngine.jar in dos then it saves the
results in Results.xml

Then i get the results then list them in a treeview.

Have a wonderfull day.
 
K

Ken Cox - Microsoft MVP

Salut,

First, I'd change Request() to something else. In ASP.NET you're likely
referring to the Request object.

I was playing with this code by Brendan Tompkins that might help do what you
want:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
ARequest();
}
private void ARequest()
{
// Credit to Brendan Tompkins
//http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx
// Get the full file path
string strFilePath = Server.MapPath("request.bat");

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = Server.MapPath("");

// Start the process
System.Diagnostics.Process proc =
System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while (strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}
strm.Close();
// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";
sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();
// Close the io Streams;
sIn.Close();
sOut.Close();

// Write out the results.
string fmtStdOut = "<code>{0}</code>";
lblerror.Text = String.Format(fmtStdOut,
results.Replace(System.Environment.NewLine, "<br>"));
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Run Batch</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="lblerror" runat="server"></asp:label></div>
</form>
</body>
</html>



Hi every one,

I am trying to run a batch file using my asp.net application.

I am using the Process class to run the batch file. When I run my web
application, In the task manager, i could see cmd.exe with ASPNET as a
user. But nothing happens. It can't execute the batch file.

This is the code i am using to run the batch file:

private void Request()

{

string Fichier=Server.MapPath("")+"\\SearchEng\\request.bat";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;


proc.StartInfo.WorkingDirectory=Server.MapPath("")+"\\SearchEng\\";

proc.StartInfo.FileName=Fichier;



try

{

proc.Start();


proc.WaitForExit();

proc.Close();

}


catch(Exception ex)

{

this.lblerror.Text=ex.Message;

}

}



********************************************************************

string Fichier=Server.MapPath("")+\\SearchEng\\request.bat;
Response.Write(Fichier)

It does return the physical path of the file starting from
C:\myApplication\SearchEng\request.bat.

But if don't run the file from the physical path (example : if fichier
= SearchEng\request.bat) it does give me an error : The system cannot
find the file specified.

I did another test: When i manually click on the request.bat file it
does execute and returns me the right answers.

This is what i get on my dos screen when i run the bat file with my
code.

C:\myApplication\SearchEng>set
path="C:\myApplication\SearchEng";C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program
Files\ATI Technologies\ATI Control Panel;C:\Program Files\Microsoft SQL
Server\80\Tools\BINN;C:\bin C:\myApplication\SearchEng>java -jar
"C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps IPSearch
Version 1.22 All rights reserved ¬ 2005 my company Researche in
progress ...... Error Results are saved in the Results.xml

You can see research is in progress. I think the Process end before the
SearchEngine.jar finishes executing.

But when i run it manually i don't no error, Very strange

I tried this proc.WaitForExit(900000) or proc.WaitForExit() but still
ended without success.

The content of my batch file is :
set path="C:\myApplication\SearchEng";%path%
java -jar "C:\myApplication\SearchEng\SearchEngine.jar" GOOGLE/EPS/ gps

This batch file execute : SearchEngine.jar in dos then it saves the
results in Results.xml

Then i get the results then list them in a treeview.

Have a wonderfull day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top