当程序加载时,我想检查一个文件,如果文件存在,我想继续,如果没有,我希望它不打开主表单,打开一个表单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!");
}
}但它打开了这两种形式。有人能帮帮我吗?谢谢。
发布于 2013-07-25 12:45:45
在我看来,你应该在应用程序启动时这样做。现在,您正在第一个表单的负载中执行此操作,这是您不希望打开的。所以,就像这样:
[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());
}
}https://stackoverflow.com/questions/17858374
复制相似问题