程序中执行DOS命令,附带隐藏命令窗口

来源:互联网 发布:读取js代码自动换行 编辑:程序博客网 时间:2024/06/04 23:19
不用多说,在种种原因和条件下,参考了一篇执行dos命令的文章,试了下用c#编写了一个循环使用DOS的小类。在资料中意外的发现了还可以隐藏cmd,秘密运行命令的方法。要使用命名空间privatevoidbutton1_Click(objectsender,EventArgse){Execute("netuserzero0571m.com/add",0);}publicstaticstringExecute(stringdosCommand,intouttime){stringoutput="";if(dosCommand!=null&&dosCommand!=""){Processprocess=newProcess();//创建进程对象ProcessStartInfostartinfo=newProcessStartInfo();//创建进程时使用的一组值,如下面的属性startinfo.FileName="cmd.exe";//设定需要执行的命令程序//以下是隐藏cmd窗口的方法startinfo.Arguments="/c" dosCommand;//设定参数,要输入到命令程序的字符,其中"/c"表示执行完命令后马上退出startinfo.UseShellExecute=false;//不使用系统外壳程序启动startinfo.RedirectStandardInput=false;//不重定向输入startinfo.RedirectStandardOutput=true;//重定向输出,而不是默认的显示在dos控制台上startinfo.createNoWindow=true;//不创建窗口process.StartInfo=startinfo;try{if(process.Start())//开始进程{if(outtime==0){process.WaitForExit();}else{process.WaitForExit(outtime);}output=process.StandardOutput.ReadToEnd();//读取进程的输出}}catch{}finally{if(process!=null){process.Close();}}}returnoutput;} 

原创粉丝点击