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所在目录中而不是批处理所在目录了!
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
-
网友评论