C# 从代码执行 CMD 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1255909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute CMD command from code
提问by Jake
In C# WPF: I want to execute a CMD command, how exactly can I execute a cmd command programmatically?
在 C# WPF 中:我想执行 CMD 命令,我究竟如何以编程方式执行 cmd 命令?
回答by JP Alioto
Are you asking how to bring up a command windows? If so, you can use the Process object...
您是在问如何调出命令窗口吗?如果是这样,您可以使用Process 对象...
Process.Start("cmd");
回答by Mitch Wheat
Using Process.Start:
using System.Diagnostics;
class Program
{
static void Main()
{
Process.Start("example.txt");
}
}
回答by Acron
Argh :D not the fastest
啊 :D 不是最快的
Process.Start("notepad C:\test.txt");
回答by Andreas Grech
Here's a simple example :
这是一个简单的例子:
Process.Start("cmd","/C copy c:\file.txt lpt1");
回答by Carlo
How about you creat a batch file with the command you want, and call it with Process.Start
你如何用你想要的命令创建一个批处理文件,并用 Process.Start 调用它
dir.bat content:
dir.bat 内容:
dir
then call:
然后调用:
Process.Start("dir.bat");
Will call the bat file and execute the dir
会调用bat文件并执行dir
回答by Ashley Davis
As mentioned by the other answers you can use:
正如其他答案所述,您可以使用:
Process.Start("notepad somefile.txt");
However, there is another way.
然而,还有另一种方式。
You can instance a Process object and call the Start instance method:
您可以实例化 Process 对象并调用 Start 实例方法:
Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.WorkingDirectory = "c:\temp";
process.StartInfo.Arguments = "somefile.txt";
process.Start();
Doing it this way allows you to configure more options before starting the process. The Process object also allows you to retrieve information about the process whilst it is executing and it will give you a notification (via the Exited event) when the process has finished.
这样做可以让您在开始该过程之前配置更多选项。Process 对象还允许您在进程执行时检索有关进程的信息,并在进程完成时向您发出通知(通过 Exited 事件)。
Addition: Don't forget to set 'process.EnableRaisingEvents' to 'true' if you want to hook the 'Exited' event.
补充:如果你想挂钩 'Exited' 事件,不要忘记将 'process.EnableRaisingEvents' 设置为 'true'。
回答by Zviadi
if you want to start application with cmd use this code:
如果要使用 cmd 启动应用程序,请使用以下代码:
string YourApplicationPath = "C:\Program Files\App\MyApp.exe"
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.FileName = "cmd.exe";
processInfo.WorkingDirectory = Path.GetDirectoryName(YourApplicationPath);
processInfo.Arguments = "/c START " + Path.GetFileName(YourApplicationPath);
Process.Start(processInfo);
回答by mfatihk
You can use this to work cmdin C#:
您可以使用它在C# 中使用cmd:
ProcessStartInfo proStart = new ProcessStartInfo();
Process pro = new Process();
proStart.FileName = "cmd.exe";
proStart.WorkingDirectory = @"D:\...";
string arg = "/c your_argument";
proStart.Arguments = arg;
proStart.WindowStyle = ProcessWindowStyle.Hidden;
pro.StartInfo = pro;
pro.Start();
Don't forget to write /cbefore your argument!!
不要忘记在你的论点之前写上/c!!
回答by Matt
In addition to the answers above, you could use a small extension method:
除了上面的答案,您还可以使用一个小的扩展方法:
public static class Extensions
{
public static void Run(this string fileName,
string workingDir=null, params string[] arguments)
{
using (var p = new Process())
{
var args = p.StartInfo;
args.FileName = fileName;
if (workingDir!=null) args.WorkingDirectory = workingDir;
if (arguments != null && arguments.Any())
args.Arguments = string.Join(" ", arguments).Trim();
else if (fileName.ToLowerInvariant() == "explorer")
args.Arguments = args.WorkingDirectory;
p.Start();
}
}
}
and use it like so:
并像这样使用它:
// open explorer window with given path
"Explorer".Run(path);
// open a shell (remanins open)
"cmd".Run(path, "/K");
回答by Mayank Tripathi
You can do like below:
你可以像下面这样做:
var command = "Put your command here";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.WorkingDirectory = @"C:\Program Files\IIS\Microsoft Web Deploy V3";
procStartInfo.CreateNoWindow = true; //whether you want to display the command window
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
label1.Text = result.ToString();