【问题标题】:run console application in C# with parameters在 C# 中使用参数运行控制台应用程序
【发布时间】:2011-09-24 14:44:56
【问题描述】:

如何在 C# 中运行控制台应用程序,将参数传递给它,并以 Unicode 格式获取应用程序的结果? Console.WriteLine 用于控制台应用程序。 重要的一点是在控制台应用程序中写入 Unicode。

【问题讨论】:

  • 很多帖子。控制台仅支持 8 位字符编码。从技术上讲,您可以将 Console.OutputEncoding 切换为 utf8。如果您在没有重定向的情况下运行它,那看起来不会很好。改用文件是个好主意。

标签: c# console-application


【解决方案1】:

来自MSDN的样本

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

【讨论】:

    【解决方案2】:

    查看Process.Start():

    MSDN - Process.Start Method

    您的代码可能类似于:

    var process = Process.Start(pathToProgram, argsString);
    
    process.WaitForExit();
    
    var exitCode = process.ExitCode;
    

    如果“控制台应用程序的结果”是指程序在运行时向控制台的任何输出...您需要查看文档并弄清楚如何将程序的输出从控制台到另一个流。

    【讨论】:

      【解决方案3】:

      试试下面的代码,这里的“Amay”是一个参数。

       System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(@"E:\\ConsoleApplicationt\bin\Debug\ConsoleApplicationt.exe", "Amay");
      
       System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
      

      【讨论】:

        【解决方案4】:

        这里http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx你可以看到如何从控制台应用程序读取输出你从Process.Start()开始。

        【讨论】:

          【解决方案5】:

          看看Process 类。您可以使用 Process.Start("myexe.exe"); 调用任何可执行文件;

          【讨论】:

            【解决方案6】:

            根据您的使用情况,您应该小心,其他一些示例可能会出现问题。有关编写自己的代码的常见错误,请阅读“How to use System.Diagnostics.Process correctly

            要使用一个库,这里有一个:http://csharptest.net/browse/src/Library/Processes 附简要使用指南:“Using the ProcessRunner class

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-10-16
              • 2013-06-10
              • 1970-01-01
              相关资源
              最近更新 更多