Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载表单时,打开表单2并关闭表单1_C#_.net_Winforms_Forms - Fatal编程技术网

C# 加载表单时,打开表单2并关闭表单1

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

当程序加载时,我想检查文件,如果文件存在,我想继续,如果不存在,我不想打开主窗体并打开窗体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());
    }
}