我如何在C#中执行一个cmd,然后在同一个窗口中执行下面另一个命令? [英] How do I execute a cmd in C#, then in the same window execute another command that follows?

查看:805
本文介绍了我如何在C#中执行一个cmd,然后在同一个窗口中执行下面另一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键什么即时试图做到的是,基本上将活动分区1点击一个程序,节省了使用的命令提示符等方面的努力,时间和技能。

Right what im trying to accomplish is a program that basically sets the active partition in 1 click, saving the effort time and skill of using cmd prompt etc.

我看过成System.Management命名空间,但不能工作了如何使用它:(

I have looked into the System.Management name space but couldn't work out how to use it :(

所以,我必须使出使用CMD,我有一个模块应用程序C#编写的,基本上我想要运行DISKPART,然后启动diskpart在cmd窗口,那么我要问它选择磁盘0,其次是选择分区1终于其次是积极的。

So i have resorted to using CMD, i have got a module application written in C# and basically i want to run "DISKPART" which then starts the diskpart in the cmd window, then i want to ask it to "Select disk 0" followed by "select partition 1" finally followed by "active".

在CMD这样做自己工作正常,但与应用程序的证明是尴尬:(什么香港专业教育学院设法得到它要做的就是运行DiskPart的罚款在一个窗口的Process.Start ,然后把它打开一个新窗口,并运行在未来一段代码,而是因为新的窗口处理不当运行CMD DISKPART它不工作> :(

Doing this in CMD yourself works fine but with an application its proved to be awkward :( What ive managed to get it to do is run DiskPart fine in one window with Process.Start, then get it to open a new window and run the next piece of code but because the new window hasnt ran the diskpart cmd it doesnt work >:(

有什么建议?

谢谢!

灰分

推荐答案

只要你是不是对输出做决策,你可以在你的C#应用​​程序建立一个批处理文件,并启动通过的Process.Start(...)

As long as you aren't making decisions on the output, you could build a batch file in your C# app and start that via Process.Start(...).

您需要生成两个文件。

首先 runDiskPart.bat


diskpart /s myScript.dp

myScript.dp


...some commands...
exit

显然,名称是完全任意但 / S 指令需要引用你的第二个文件的名称。

Obviously the names are completely arbitrary but the /s directive needs to reference the name of your second file.

这篇关于我如何在C#中执行一个cmd,然后在同一个窗口中执行下面另一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆