Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 Prototype教程 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 尚未分类 >

C#调用批处理

时间:2010-07-21 00:26来源:未知 作者:admin 点击:
<%@ 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#调用批处理-Mzwu.Com</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;//设置为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();
        p.WaitForExit();
        Label1.Text = p.StandardOutput.ReadToEnd();
        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


说明:当批处理和aspx不在同一目录中时,最好用WorkingDirectory设置启动的进程的初始目录为批处理所在目录,否则如上例中批处理新建的目录就应在aspx所在目录中而不是批处理所在目录了!
 

(责任编辑:admin)
Tags:批处理
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价