我编写了一个应用程序来检查启动时是否存在某些文件和文件夹.如果他们不这样做,我打电话this.Close();(因为我想退出整个申请).但是,我收到一个错误:ObjectDisposedErroron Application.Run(new MainForm());.我认为这与在成功加载表单之前调用.Close有关.我正在从MainForm.cs的公共MainForm()函数执行这些启动检查.这是正确的还是我需要把它放在别的地方?
我尝试过:使用委托运行它并调用:
public void CloseApplication() {
if (this.InvokeRequired) {
this.Invoke(new CloseGameDelegate(CloseApplication));
} else {
this.Close();
}
}
public delegate void CloseGameDelegate();
Run Code Online (Sandbox Code Playgroud)
这仍然给我同样的错误.
使用Application.Exit():这只是打开表单.
如果您正在查找特定文件,并且如果找不到它们甚至不想启动应用程序,则可以在"Program.cs"文件中执行检查.
MainForm如果找不到文件,请不要打扰创建和打开:
static void Main()
{
...
var isFileFound = LookForSomeFiles();
if (!isFileFound)
{
MessageBox.Show("Important files were moved or removed. Please contact support.");
return;
}
Application.Run(new MainForm());
}
private static bool LookForSomeFiles()
{
// perform your check
}
Run Code Online (Sandbox Code Playgroud)