欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# 批处理调用方法

时间:2021-06-29 08:27:17|栏目:.NET代码|点击:
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#调用批处理-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;//设置为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^<br^>服了你,这么有耐心 & exit
goto loop

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

上一篇:ASP.Net获取客户端网卡MAC的小例子

栏    目:.NET代码

下一篇:C#发送数据到剪贴板及从剪贴板中取数据的方法

本文标题:C# 批处理调用方法

本文地址:http://www.codeinn.net/misctech/149795.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有