【问题标题】:Execute CMD command from code从代码执行 CMD 命令
【发布时间】:2010-11-18 08:39:04
【问题描述】:

在 C# WPF 中:我想执行 CMD 命令,我该如何以编程方式执行 cmd 命令?

【问题讨论】:

  • 我不太了解 c、c++ 或 c#,但我建议将其编程为将代码写入批处理文件,运行批处理文件,然后删除批处理文件。

标签: c# wpf dll command-line cmd


【解决方案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();

【讨论】:

    【解决方案2】:

    除了上面的答案,你还可以使用一个小的扩展方法:

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

    【讨论】:

      【解决方案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!!

      【讨论】:

      • "pro.StartInfo = pro;"应该是“pro.StartInfo = proStart;”是吗?
      【解决方案4】:

      如果您想使用 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);
      

      【讨论】:

        【解决方案5】:

        正如您可以使用的其他答案所述:

          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”。

        【讨论】:

          【解决方案6】:

          你如何用你想要的命令创建一个批处理文件,然后用 Process.Start 调用它

          dir.bat 内容:

          dir
          

          然后调用:

          Process.Start("dir.bat");
          

          会调用bat文件并执行目录

          【讨论】:

          • 好主意!我做了 Process.Start("test.bat") 但弹出一个错误,说“System.dll 中发生了'System.ComponentModel.Win32Exception' 类型的未处理异常”。有什么想法吗?
          • 哦,nvm,修好了。非常感谢卡洛。真是个好主意,帮了大忙。
          【解决方案7】:

          这是一个简单的例子:

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

          【讨论】:

          • 我正在尝试,但第二个参数,该参数并没有真正传递到命令窗口,至少在 Windows 8.1 中没有
          • @William 我在 Windows 10 中对此进行了测试,它工作正常。
          • 亲爱的@jan'splitek 我100% 确定这个代码。你可以使用 run 打开 cmd 吗? (您的局部变量可能已损坏)
          【解决方案8】:

          啊 :D 不是最快的

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

          【讨论】:

            【解决方案9】:

            使用Process.Start

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

            【讨论】:

              【解决方案10】:

              您是在问如何调出命令窗口吗?如果是这样,您可以使用Process object ...

              Process.Start("cmd");
              

              【讨论】:

                猜你喜欢
                • 2019-11-08
                • 1970-01-01
                • 2011-07-24
                • 2016-06-17
                • 2017-11-21
                • 2023-01-25
                • 2012-07-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多