教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c# 批处理调用方法(实例)

c# 批处理调用方法(实例)

发布时间:2015-12-07   编辑:jiaochengji.com
c# 批处理调用方法介绍,通过一个简单的例子,教大家如何使用c#调用windows下的批处理文件。有需要的朋友可以参考下。

c# 批处理调用方法介绍,通过一个简单的例子,教大家如何使用c#调用windows下的批处理文件。
有需要的朋友可以参考下。

1、前台页面 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# 批处理调用</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>

2、后台代码 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();
}
}

3、批处理文件 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

备注:当批处理和aspx不在同一目录中时,最好用WorkingDirectory设置启动的进程的初始目录为批处理所在目录,否则批处理新建的目录就应在aspx所在目录中,而非批处理所在目录。这点大家务必留心。

您可能感兴趣的文章:
什么是批处理
dos批处理静默执行的方法(不弹出窗口)
创建目录结构的批处理脚本
c# 批处理调用方法(实例)
使用批处理修改本机HOST的方法
批处理BAT生成随机数调用随机网站内容的方法
c# 调用批处理(bat)的实现代码
有关mysql备份的批处理(bat)脚本
批处理概念与方法
dos命令之echo命令介绍

[关闭]
~ ~