Bat.aspx:
Program code
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Bat.aspx.cs" inherits= "Bat"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>c# Call Batch-jb51.net</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label" width= "304px" ></asp:Label></div>
</form>
</body>
Bat.aspx.cs:
Program Code Program code
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Diagnostics;
public partial class Bat:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
//
}
protected void Button1_Click (object sender, EventArgs e)
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
P.startinfo.useshellexecute = false;
P.startinfo.createnowindow = true;//set to False will see the program window
P.startinfo.windowstyle = window state when the process is started processwindowstyle.hidden;//
P.startinfo.redirectstandardoutput = true;
p.StartInfo.FileName = Server.MapPath ("A.bat");
p.StartInfo.FileName = @ "E:\a.bat";//If A.bat is in System32 folder, just fill in the file name here
P.startinfo.workingdirectory = @ "E:\";
p.startinfo.arguments = Server.URLEncode (TextBox1.Text);
P.start ();
Label1.Text = P.standardoutput.readtoend ();
p.WaitForExit ();
P.close ();
}
}
A.bat:
Program code
Copy Code code as follows:
@echo off
MD%random%
Set I=1
: Loop
Ping 1-n 1-w 1000 2>nul 1>nul
set/a i=%i%+1
If%i%==20 echo returns a value:%1^<br^> you, so patient & exit
Goto Loop
Note: When batch processing and ASPX are not in the same directory, it is best to set the initial directory for the process that started with workingdirectory to the directory where the batch is processed, otherwise the batch-created directory in the previous example should be in the directory of ASPX instead of the directory where the batch is located!