加载表单时,打开表单2并关闭表单1
本文关键字:表单 加载 | 更新日期: 2023-09-27 18:24:58
当程序加载时,我想检查文件,如果文件存在,我想继续,如果不存在,我希望它不打开主窗体并打开窗体2。
这就是我目前所拥有的:
private void Home_Load(object sender, EventArgs e)
{
string path = @"c:'Path'Path2'file.txt";
if (!File.Exists(path))
{
MessageBox.Show("File not found!");
form2 f = new form2();
f.Show();
this.Hide();
}
else
{
MessageBox.Show("File found!");
}
}
但它打开了两种形式。有人能帮我吗?谢谢
在我看来,你应该在应用程序启动时这样做。现在你正在以第一种形式做这件事,你不想打开它。所以,像这样的东西:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string path = @"c:'Path'Path2'file.txt";
if (!File.Exists(path))
{
Application.Run(new form2());
}
else
{
Application.Run(new form1());
}
}