在完成处理后退出控制台应用程序



我有一个代码,启动一个进程,其中包含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);

你可以这样试试

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

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

您是否尝试删除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
}

这应该可以为您工作:

Environment.Exit(0);

最新更新