Skip to main content
 首页 » 编程设计

c#之如何调用 main 方法或重启应用程序控制台

2024年02月23日12telwanggs

当发生异常时我想重新启动所有处理或启动 Main 方法,在此其他方法之后:

public void DisplayMessage(string message) {  
    Console.WriteLine(message, "Rebuild Log Files");  
    Console.WriteLine(" Press Enter to finish, or R to restar the program...");  
    string restart = Console.ReadLine();  
    if(restart.ToUpper() == "R") {  
        //Call the Main method or restart the app  
    }  
    Console.ReadKey();  
} 

注意:main方法中包含一些用户写入的数据。

我该怎么做?

请您参考如下方法:

好的,你有一个主要的

void main(...) 
{ 
    some code 
} 

你需要做的就是......

void main() 
{ 
    runStartUpCode(); 
} 
 
void runStartUpCode() 
{ 
    some code 
} 

然后当你需要重新启动代码时,再次调用runStartUpCode()。