【问题标题】:How to use more DOS commands in C#如何在 C# 中使用更多的 DOS 命令
【发布时间】:2011-10-23 08:58:53
【问题描述】:

我在 DOS 中有大约 7 个命令,我想在我的 C# 程序中运行它们。我能做到吗:

System.Diagnostics.Process.Start("cmd.exe", "my more commands here");

编辑: 我正在制作将运行 g++ 的小应用程序。这现在正确吗?:

 System.Diagnostics.Process.Start("cmd.exe", "/k cd C:\\Alps\\compiler\\ /k g++ C:\\Alps\\" + project_name + "\\Debug\\Main.cpp");

编译命令:

g++ -c C:\Alps\here_is_projectname\Debug\Main.cpp -o main.o

【问题讨论】:

    标签: c# dos process


    【解决方案1】:
    cmd.exe /k <command>
    cmd.exe /c <command>
    

    都是有效的。

    • /k 将执行命令并为您留下一个空提示(如果您只想执行反馈,在您的应用程序中可能不太理想。)
    • /c 将执行命令并在完成后关闭窗口。

    如果您希望从特定目录执行命令,您可以这样做:

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
    p.StartInfo.Arguments = String.Format(@"/c g++ ""C:\Alps\{0}\Debug\Main.cpp""", project_name);
    p.StartInfo.WorkingDirectory = @"C:\Alps\compiler";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.ErrorDialog = false;
    p.Start();
    

    【讨论】:

    • /k 对我来说更好。谢谢。还有一个小问题:cmd 会在 C# 应用程序路径中启动(在 vs 默认 mydocs .....\bin\debug 中)吗?我需要更多命令,所以 /c 对我不利。
    • @FrewCen:说实话,我不记得了。但是,您可以修改 ProcessStartInfo.WorkingPath 让它在您想要的任何地方运行。
    • @FrewCen:很抱歉,我没有在StartInfo.Arguments 分配中包含/c 标志。
    • 好的,但是请您查看 ful 命令并编辑您的代码,因为您有很多 '"' 而我不知道如何在该行中编辑该代码。我可以在哪里添加-c 和 -o ?
    • @FrewCen:那你为什么不直接打电话给g++.exe呢?为什么要先通过cmd.exe 传递命令?
    【解决方案2】:

    是的,您可以使用“/C”开关在命令行中传递:

    System.Diagnostics.Process.Start("cmd.exe", "/C dir");
    

    【讨论】:

    • @chibacity:对不起,编辑你的答案不是我的。我很抱歉。
    【解决方案3】:

    你也可以像下面这样......

    Process.Start(new ProcessStartInfo()
    {
     Arguments = "args",
     WorkingDirectory = "C:\SomePath",
     UseShellExecute= true,
     FileName = ".exe"
    });
    

    如果需要,processstartinfo 上还有一些选项可以重定向输入和输出

    例如..

    Process.Start(new ProcessStartInfo()
    {
     Arguments = "C:\\Alps\\" + project_name + "\\Debug\\Main.cpp",
     WorkingDirectory = "C:\\Apls\\",
     UseShellExecute= true,
     FileName = "g++.exe"
    });
    

    【讨论】:

      【解决方案4】:

      您可以启动 cmd.exe 重定向与您的命令一起流式传输的标准输入和脚。

       process.Start(...);
      
      
                          process.StandardInput.WriteLine("Dir xxxxx");
                          process.StandardInput.WriteLine("Dir yyyyy");
                          process.StandardInput.WriteLine("Dir zzzzzz");
                          process.StandardInput.WriteLine("other command(s)");
      

      当然你应该记住设置你的进程星号信息说你想要重定向输入:

       ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe);
              processStartInfo.CreateNoWindow = true;
      
              processStartInfo.ErrorDialog = false;
      
              processStartInfo.RedirectStandardInput = true;
      

      【讨论】:

      • @FrewCen 进程在我的示例中是进程的实例,而不是类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 2011-08-21
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多