C# 批處理調(diào)用方法
更新時(shí)間:2008年12月09日 15:32:30 作者:
當(dāng)批處理和aspx不在同一目錄中時(shí),最好用WorkingDirectory設(shè)置啟動(dòng)的進(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將會(huì)看到程序窗口
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//啟動(dòng)進(jìn)程時(shí)窗口狀態(tài)
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.FileName = Server.MapPath("a.bat");
p.StartInfo.FileName = @"E:\a.bat";//如果a.bat在System32文件夾中,此處只需填寫(xiě)文件名即可
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
說(shuō)明:當(dāng)批處理和aspx不在同一目錄中時(shí),最好用WorkingDirectory設(shè)置啟動(dòng)的進(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將會(huì)看到程序窗口
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//啟動(dòng)進(jìn)程時(shí)窗口狀態(tài)
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.FileName = Server.MapPath("a.bat");
p.StartInfo.FileName = @"E:\a.bat";//如果a.bat在System32文件夾中,此處只需填寫(xiě)文件名即可
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
說(shuō)明:當(dāng)批處理和aspx不在同一目錄中時(shí),最好用WorkingDirectory設(shè)置啟動(dòng)的進(jìn)程的初始目錄為批處理所在目錄,否則如上例中批處理新建的目錄就應(yīng)在aspx所在目錄中而不是批處理所在目錄了!
相關(guān)文章
C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果
這篇文章主要介紹了C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果,涉及時(shí)間函數(shù)的應(yīng)用及繪圖的方法,需要的朋友可以參考下2014-10-10
如何讓C#、VB.NET實(shí)現(xiàn)復(fù)雜的二進(jìn)制操作
VB.NET和C#屬于高級(jí)語(yǔ)言,對(duì)二進(jìn)制位操作的支持不是很好,比如沒(méi)有了移位運(yùn)算等,用的時(shí)候確實(shí)很不方便,所以在閑暇之余我重新封裝了一個(gè)用于C#、VB.NET的位操作類庫(kù),通過(guò)該類庫(kù)可以實(shí)現(xiàn)數(shù)據(jù)移位、循環(huán)移位、轉(zhuǎn)換為二進(jìn)制、將二進(jìn)制轉(zhuǎn)換為數(shù)據(jù)等2013-07-07
C#訪問(wèn)及調(diào)用類中私有成員與方法示例代碼
訪問(wèn)一個(gè)類的私有成員不是什么好做法,大家也都知道私有成員在外部是不能被訪問(wèn)的,這篇文章主要給大家介紹了關(guān)于C#訪問(wèn)及調(diào)用類中私有成員與方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
wpf將表中數(shù)據(jù)顯示到datagrid示例
這篇文章主要介紹了wpf將表中數(shù)據(jù)顯示到datagrid示例,需要的朋友可以參考下2014-02-02
C#中限制并發(fā)任務(wù)數(shù)量的高效方法與技巧分享
在C#中,處理并發(fā)操作是一項(xiàng)常見(jiàn)且強(qiáng)大的功能,尤其是在需要執(zhí)行多個(gè)任務(wù)但又希望限制同時(shí)運(yùn)行任務(wù)數(shù)量的場(chǎng)景中,本文將深入探討幾種有效的方法來(lái)限制C#中的并發(fā)任務(wù)數(shù)量,并通過(guò)具體的應(yīng)用場(chǎng)景和示例代碼展示如何實(shí)現(xiàn)這些方法,需要的朋友可以參考下2024-12-12
C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則
這篇文章介紹了C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03

