C# 批处理调用方法

当批处理和aspx不在同一目录中时,最好用WorkingDirectory设置启动的进程的初始目录为批处理所在目录,否则如上例中批处理新建的目录就应在aspx所在目录中而不是批处理所在目录了! Bat.aspx: 程序代码 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bat.aspx.cs" Inherits="Bat" %> C#调用批处理-jb51.net

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;//设置为false将会看到程序窗口 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//启动进程时窗口状态 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^ 服了你,这么有耐心 & exit goto loop 说明:当批处理和aspx不在同一目录中时,最好用WorkingDirectory设置启动的进程的初始目录为批处理所在目录,否则如上例中批处理新建的目录就应在aspx所在目录中而不是批处理所在目录了! 详细出处参考:http://www.jb51.net/article/16751.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值