2013-07-25 209 views
1

當程序加載時我想檢查文件,如果文件存在,我想繼續,如果不是,我希望它不打開主窗體,打開表單2.當窗體加載時,打開窗體2並關閉窗體1

這是我到目前爲止有:

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!"); 
    } 
} 

但它開啓了兩種形式。誰能幫幫我嗎?謝謝。

回答

3

對我來說,你應該在應用程序啓動時做到這一點。現在你在第一種形式的負載中這樣做,你不想打開。所以,像這樣:

[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()); 
    } 
} 
+0

非常感謝!有效! –

+0

然後將其標記爲答案! –

相關問題