c#怎么调用cmd的命令呀?或者说怎么向cmd中写入命令?

我想到了一个方法,就是把cmd命令写入批处理,然后c#用process.start();就可以了,但由于需要,这要两个文件,我现在必须一个文件就得搞定,所以这个方法大家就... 我想到了一个方法,就是把cmd命令写入批处理,然后c#用process.start();就可以了,但由于需要,这要两个文件,我现在必须一个文件就得搞定,所以这个方法大家就别谈了。

当然如果是像关机之类的名令也好办Process.start(shutdown -s -t 0)
可是我现在要的命令是
@ECHO OFF
ECHO Set WshShell = Wscript.CreateObject("Wscript.Shell") >%temp%\tmp.vbs
ECHO strAllUsersStartup= WshShell.SpecialFolders("AllUsersStartup") >>%temp%\tmp.vbs
CMD /c "ECHO ^Set MyLink = WshShell.CreateShortcut(strAllUsersStartup ^& "\start.lnk")" >>%temp%\tmp.vbs"
ECHO MyLink.TargetPath = "C:\MSIM" >>%temp%\tmp.vbs
ECHO MyLink.Save >>%temp%\tmp.vbs
cscript /nologo %temp%\tmp.vbs
DEL /q /s %temp%\tmp.vbs 2>nul 1>nul
这么长,恐怕也很难实现。
所以办法是用一个string储存上面这么长的文字,然后打开cmd,用string写入。
在网上找了下
using System;
string str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
System.Diagnostics.Process pro = new System.Diagnostics.Process();
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.CreateNoWindow = true;

pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pro.Start();
pro.StandardInput.WriteLine(str);//在这位里你需要写入执行命令行。。你的意思没有表达清楚啊
pro.StandardInput.WriteLine("exit");
这个命令似乎没用呀?
展开
 我来答
匿名用户
推荐于2017-09-08
展开全部
var processStartInfo = new ProcessStartInfo(fileName, arguments);
 
    processStartInfo.UseShellExecute = false;
    processStartInfo.ErrorDialog = false;
    //是否显示窗口
    processStartInfo.CreateNoWindow = true;
    //重定向标准错误
    processStartInfo.RedirectStandardError = true;
    //重定向标准输入
    processStartInfo.RedirectStandardInput = true;
    //重定向标准输出
    processStartInfo.RedirectStandardOutput = true;



请看下面这个链接,文章里有详细的代码和解释


http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

更多追问追答
追问
你能说详细点吗?英文我看不懂。
追答

直接看代码 还不详细啊! 

 public static void RunCmdWithoutResult(string file, string command, bool wait)
        {
            //创建实例
            Process p = new Process();
            //设定调用的程序名,不是系统目录的需要完整路径
            p.StartInfo.FileName = file;
//传入执行参数
            p.StartInfo.Arguments = " " + command; 
            p.StartInfo.UseShellExecute = false;
//是否重定向标准输入
            p.StartInfo.RedirectStandardInput = false; 
//是否重定向标准转出
            p.StartInfo.RedirectStandardOutput = false;
//是否重定向错误
            p.StartInfo.RedirectStandardError = false;
//执行时是不是显示窗口
            p.StartInfo.CreateNoWindow = true;
//启动
            p.Start(); 
            if (wait)
            {
//是不是需要等待结束
                p.WaitForExit();
            }
            p.Close();
        }
        
        把上面你要执行的bat写到一个文件中
        
        然后可以这样调用 
        
        
      RunCmdWithoutResult("cmd.ex",bat文件的完整路径,true或false)
百度网友7176e78
2016-01-14 · TA获得超过168个赞
知道小有建树答主
回答量:225
采纳率:0%
帮助的人:84.8万
展开全部
醉了,echo那几句你就直接用C#来写文件啊,FileStream啊
而且你这么一大段的意思就是把一些文字输出到tmp.vbs然后执行并删除
全部都用C#就好了弄cmd干毛啊!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zp_0409
2019-06-29 · TA获得超过175个赞
知道答主
回答量:40
采纳率:100%
帮助的人:13.2万
展开全部
我觉得最简单的是这种方式:写个批处理,然后用process.start()调用。手机写的,测试时注重大小写!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式