Ich muss die Konsole schließen, wenn der Benutzer eine Menüoption auswählt.
Ich habe versucht, close()
zu verwenden, aber es hat nicht funktioniert.
wie kann ich das machen?
Environment.Exit
und Application.Exit
Environment.Exit(0)
ist sauberer.
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
Mit Schließen meinen Sie, dass die aktuelle Instanz der Konsolenanwendung geschlossen werden soll oder der Anwendungsprozess beendet werden soll. Verpasste den wichtigen Exit-Code:
Environment.Exit(0);
Oder um die aktuelle Instanz des Formulars zu schließen:
this.Close();
Nützliches link .
Sie können dies versuchen
Application.Exit();
//How to start another application from the current application
Process runProg = new Process();
runProg.StartInfo.FileName = pathToFile; //the path of the application
runProg.StartInfo.Arguments = genArgs; //any arguments you want to pass
runProg.StartInfo.CreateNoWindow = true;
runProg.Start();
//How to end the same application from the current application
int IDstring = System.Convert.ToInt32(runProg.Id.ToString());
Process tempProc = Process.GetProcessById(IDstring);
tempProc.CloseMainWindow();
tempProc.WaitForExit();
Sie haben also nicht gesagt, dass Sie die Anwendung beenden oder abrupt beenden möchten. Als weitere Option können Sie die Antwortschleife möglicherweise einfach elegant beenden. (Ich gehe davon aus, dass Sie eine while-Schleife haben, die auf Benutzeranweisungen wartet. Dies ist ein Code aus einem Projekt, das ich gerade geschrieben habe.
Console.WriteLine("College File Processor");
Console.WriteLine("*************************************");
Console.WriteLine("(H)elp");
Console.WriteLine("Process (W)orkouts");
Console.WriteLine("Process (I)nterviews");
Console.WriteLine("Process (P)ro Days");
Console.WriteLine("(S)tart Processing");
Console.WriteLine("E(x)it");
Console.WriteLine("*************************************");
string response = "";
string videotype = "";
bool starting = false;
bool exiting = false;
response = Console.ReadLine();
while ( response != "" )
{
switch ( response )
{
case "H":
case "h":
DisplayHelp();
break;
case "W":
case "w":
Console.WriteLine("Video Type set to Workout");
videotype = "W";
break;
case "I":
case "i":
Console.WriteLine("Video Type set to Interview");
videotype = "I";
break;
case "P":
case "p":
Console.WriteLine("Video Type set to Pro Day");
videotype = "P";
break;
case "S":
case "s":
if ( videotype == "" )
{
Console.WriteLine("Please Select Video Type Before Starting");
}
else
{
Console.WriteLine("Starting...");
starting = true;
}
break;
case "E":
case "e":
Console.WriteLine("Good Bye!");
System.Threading.Thread.Sleep(100);
exiting = true;
break;
}
if ( starting || exiting)
{
break;
}
else
{
response = Console.ReadLine();
}
}
if ( starting )
{
ProcessFiles();
}