C# 加载表单时,打开表单2并关闭表单1
当程序加载时,我想检查文件,如果文件存在,我想继续,如果不存在,我不想打开主窗体并打开窗体2 这就是我到目前为止所做的:C# 加载表单时,打开表单2并关闭表单1,c#,.net,winforms,forms,C#,.net,Winforms,Forms,当程序加载时,我想检查文件,如果文件存在,我想继续,如果不存在,我不想打开主窗体并打开窗体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
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());
}
}