欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 批处理调用方法

程序员文章站 2022-06-30 09:55:59
bat.aspx: 程序代码 复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codefil...
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所在目录中而不是批处理所在目录了!