从代码中执行CMD命令

30
在C# WPF中:我想执行一个CMD命令,如何在程序中编程执行CMD命令?

我对 C、C++ 或 C# 不是很了解,但我建议编程将代码写入批处理文件、运行批处理文件,然后删除批处理文件。 - Johnny G Gaming
10个回答

38

这里有一个简单的例子:

Process.Start("cmd","/C copy c:\\file.txt lpt1");

3
我在尝试这个,但是第二个参数,也就是参数并没有真正传递到命令行窗口,至少在Windows 8.1上不是这样。 - William
@William 我在 Windows 10 上测试过了,它可以正常工作。 - M at
亲爱的@jan'splitek,我对这段代码有 100% 的把握。你可以使用“运行”打开命令提示符吗?(你的本地变量可能已经损坏) - M at

22

正如其他答案所提到的,您可以使用:

  Process.Start("notepad somefile.txt");

然而,还有另一种方法。

您可以实例化一个Process对象并调用Start实例方法:

  Process process = new Process();
  process.StartInfo.FileName = "notepad.exe";
  process.StartInfo.WorkingDirectory = "c:\temp";
  process.StartInfo.Arguments = "somefile.txt";
  process.Start();

这样做可以让您在启动进程之前配置更多选项。Process对象还允许您在执行过程中检索有关进程的信息,并在进程完成时通过Exited事件通知您。

另外,如果想要挂钩Exited事件,请不要忘记将 'process.EnableRaisingEvents' 设置为 'true'。


11

如果您想使用命令提示符启动应用程序,请使用此代码:

如果你想要用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);

10

使用Process.Start

using System.Diagnostics;

class Program
{
    static void Main()
    {
        Process.Start("example.txt");
    }
}

4
如何创建一个批处理文件并用Process.Start调用它呢?
你可以按照以下步骤:
1. 创建一个包含所需命令的批处理文件。 2. 使用Process.Start调用该批处理文件。
dir.bat内容如下:
dir

接下来调用:

Process.Start("dir.bat");

将调用批处理文件并执行dir命令


太好了!我执行了Process.Start("test.bat"),但是出现了一个错误,显示“在System.dll中发生了未处理的类型'System.ComponentModel.Win32Exception'异常”。有什么想法吗? - Jake
1
哦,不用了,我已经解决了。非常感谢,Carlo。这个想法真的很好,帮了我很多忙。 - Jake

3
您可以使用以下代码在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();

不要忘记在参数之前写上/c!!

1
"pro.StartInfo = pro;" 应该改为 "pro.StartInfo = proStart;",对吗? - amalgamate

2

哎呀:D,速度不是最快的

Process.Start("notepad C:\test.txt");

1
你可以像下面这样做:

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();

1

您是在询问如何打开命令行窗口吗?如果是的话,您可以使用进程对象...

Process.Start("cmd");

0
除了上面的答案,您还可以使用一个小扩展方法:
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();
        }
    }
}

然后像这样使用它:

// open explorer window with given path
"Explorer".Run(path);   

// open a shell (remanins open)
"cmd".Run(path, "/K");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接