471,320 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,320 software developers and data experts.

How can i run a batch file asp.net

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.b at";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maxi mized;
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.ba t;
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\syste m32;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.

Feb 3 '06 #1
3 4117
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

<em******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.b at";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maxi mized;
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.ba t;
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\syste m32;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.
Feb 3 '06 #2
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>

<em******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.b at";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maxi mized;
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.ba t;
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\syste m32;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.
Feb 3 '06 #3
string Fichier=Server.MapPath(@"SearchEng\request.bat");
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"em******@hotmail.com" wrote:
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.b at";

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

proc.StartInfo.WindowStyle=ProcessWindowStyle.Maxi mized;
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.ba t;
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\syste m32;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.

Feb 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by SkySea | last post: by
6 posts views Thread by Charles Neitzel | last post: by
14 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.