【问题标题】:Exit console application after it is done processing完成处理后退出控制台应用程序
【发布时间】:2016-01-04 03:39:44
【问题描述】:

我的代码启动了一个包含 3 个由空格分隔的参数的进程。

 ProcessStartInfo info = new ProcessStartInfo();
 info.FileName = exeLauncher;
 info.Arguments = path + " " + exeName + " " + restartNeeded;
 Process process = new Process();
 Process.Start(info);

我正在解析我开始的进程的参数并进行一些处理。

static void Main(string[] args)
{

    Console.WriteLine(args[0]);
    Console.WriteLine(args[1]);
    Console.WriteLine(args[2]);

    //some more processing here

    Console.ReadLine();
}

处理后,我希望控制台窗口自行关闭。我曾尝试使用像这样的/c 参数,但它只是将其解释为纯字符串。

info.Arguments = "/c" + path + " " + exeName + " " + restartNeeded;

我也尝试将参数括在"" 双引号中,但它不起作用。

info.Arguments = string.Format("/c \"{0} {1} {2}\"", path, exeName, restartNeeded);

【问题讨论】:

  • 为什么不删除会关闭控制台的Console.ReadLine();

标签: c# command-line process arguments


【解决方案1】:

您是否尝试删除 Console.ReadLine(); 并返回 int 以了解它是否成功执行,例如:

static int Main(string[] args)
{

    Console.WriteLine(args[0]);
    Console.WriteLine(args[1]);
    Console.WriteLine(args[2]);

    //some more processing here

    return 0; // if success or > 1 for errors
}

【讨论】:

    【解决方案2】:

    这应该适合你:

    Environment.Exit(0);
    

    【讨论】:

      【解决方案3】:

      你可以这样试试

      static void Main(string[] args)
      {
      
          Console.WriteLine(args[0]);
          Console.WriteLine(args[1]);
          Console.WriteLine(args[2]);
      
          //some more processing here
      
      }
      

      Console.ReadLine(); 会让你等待,直到你按下回车键,窗口才会关闭。因此,您可以删除此行以完成任务。

      【讨论】:

        猜你喜欢
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-31
        • 2011-12-27
        • 1970-01-01
        • 2013-06-08
        • 1970-01-01
        • 2014-02-17
        相关资源
        最近更新 更多