C# 批處理調(diào)用方法
更新時間:2008年12月09日 15:32:30 作者:
當(dāng)批處理和aspx不在同一目錄中時,最好用WorkingDirectory設(shè)置啟動的進(jìn)程的初始目錄為批處理所在目錄,否則如上例中批處理新建的目錄就應(yīng)在aspx所在目錄中而不是批處理所在目錄了!
Bat.aspx:
程序代碼
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>C#調(diào)用批處理-jb51.net</title>
</head>
<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>
</html>
Bat.aspx.cs:
程序代碼 程序代碼
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;//設(shè)置為false將會看到程序窗口
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//啟動進(jìn)程時窗口狀態(tài)
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.FileName = Server.MapPath("a.bat");
p.StartInfo.FileName = @"E:\a.bat";//如果a.bat在System32文件夾中,此處只需填寫文件名即可
p.StartInfo.WorkingDirectory = @"E:\";
p.StartInfo.Arguments = Server.UrlEncode(TextBox1.Text);
p.Start();
Label1.Text = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
}
a.bat:
程序代碼
@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 返回值:%1^<br^>服了你,這么有耐心 & exit
goto loop
說明:當(dāng)批處理和aspx不在同一目錄中時,最好用WorkingDirectory設(shè)置啟動的進(jìn)程的初始目錄為批處理所在目錄,否則如上例中批處理新建的目錄就應(yīng)在aspx所在目錄中而不是批處理所在目錄了!
程序代碼
復(fù)制代碼 代碼如下:
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>C#調(diào)用批處理-jb51.net</title>
</head>
<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>
</html>
Bat.aspx.cs:
程序代碼 程序代碼
復(fù)制代碼 代碼如下:
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;//設(shè)置為false將會看到程序窗口
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//啟動進(jìn)程時窗口狀態(tài)
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.FileName = Server.MapPath("a.bat");
p.StartInfo.FileName = @"E:\a.bat";//如果a.bat在System32文件夾中,此處只需填寫文件名即可
p.StartInfo.WorkingDirectory = @"E:\";
p.StartInfo.Arguments = Server.UrlEncode(TextBox1.Text);
p.Start();
Label1.Text = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
}
a.bat:
程序代碼
復(fù)制代碼 代碼如下:
@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 返回值:%1^<br^>服了你,這么有耐心 & exit
goto loop
說明:當(dāng)批處理和aspx不在同一目錄中時,最好用WorkingDirectory設(shè)置啟動的進(jìn)程的初始目錄為批處理所在目錄,否則如上例中批處理新建的目錄就應(yīng)在aspx所在目錄中而不是批處理所在目錄了!
相關(guān)文章
C#調(diào)用C++的dll兩種實現(xiàn)方式(托管與非托管)
這篇文章主要介紹了C#調(diào)用C++的dll兩種實現(xiàn)方式(托管與非托管),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法
這篇文章主要介紹了C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法,在C#中數(shù)組也是對象可以被傳遞,需要的朋友可以參考下2016-01-01C# NullReferenceException解決案例講解
這篇文章主要介紹了C# NullReferenceException解決案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08讀寫XML文件的內(nèi)容并將其顯示在ListView控件上的方法
下面小編就為大家?guī)硪黄x寫XML文件的內(nèi)容并將其顯示在ListView控件上的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率詳解
這篇文章主要給大家介紹了關(guān)于C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率,Winform?ComboBox數(shù)據(jù)綁定是指將數(shù)據(jù)源中的數(shù)據(jù)與ComboBox控件進(jìn)行關(guān)聯(lián),需要的朋友可以參考下2023-08-08C# Ado.net實現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲過程列表及參數(shù)信息示例
這篇文章主要介紹了C# Ado.net實現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲過程列表及參數(shù)信息,結(jié)合實例形式總結(jié)分析了C#針對SQLServer數(shù)據(jù)庫存儲過程及參數(shù)信息的各種常見操作技巧,需要的朋友可以參考下2019-02-02