2013-07-14 39 views
2

在我的Form1事件中,并非所有事情都已完成。只有第一行被实际执行。Form1加载事件只执行第一条语句

private void Form1_Load(object sender, EventArgs e) 
    { 
     //Save the currentUser text document's contents into the currentUser string 
     System.IO.StreamReader file = new System.IO.StreamReader("C\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt"); 
     currentUser = file.ReadLine(); 

     //Save the currentUser into the textbox 
     TBcurrentUser.Text = "The current user is " + currentUser + "."; 

     //Set the default text for the textbox 
     TBchangeTo.Text = "Whose Minecraft folder are you switching to?"; 

     //Add the default two items to the listbox 
     LBfiles.Items.Add("Minecraft folders:" + Environment.NewLine); 
     LBfiles.Items.Add("---------------------------------------------------------------------------------------------------------------------------"); 

     //Create the directory if it doesn't exist 
     if (!Directory.Exists("C:\\Users\\WoopyCat\\AppData\\Roaming\\" + folderName)) 
     { 
      Directory.CreateDirectory("C:\\Users\\WoopyCat\\AppData\\Roaming\\" + folderName); 
     } 

     //Set the default instructions in the textbox 
     TBinstructions.Text = instructions; 

     //Add each directory to the listbox 
     foreach (string value in Directory.GetDirectories("C:\\Users\\WoopyCat\\AppData\\Roaming\\" + folderName)) 
     { 
      //Remove the file location from the string 
      each = value.Replace("C:\\Users\\WoopyCat\\AppData\\Roaming\\.MCSwitcher\\", ""); 
      LBfiles.Items.Add(each + Environment.NewLine); 
     } 
    } 

表单加载时没有任何反应。如果我添加第一行,那么它实际上发生。就像我在这个事件中将消息框称为第一行一样,它就会发生。但是这段代码没有任何内容。

+1

你试图把你的代码在构造函数中,相反,右后'的InitializeComponent();'? – tinstaafl

+0

好的,那有效。非常感谢! – TheUnrealMegashark

+2

你的代码遇到了异常,而.Net只是吞下了异常,所以它看起来像在方法中间停止运行。见http://stackoverflow.com/questions/3209706/why-the-form-load-cant-catch-exception – tia

回答

2

变化

System.IO.StreamReader file = new System.IO.StreamReader("C\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt"); 

System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt"); 

你忘了结肠