2016-04-20 57 views
0

我有多個文本文件讀入字符串數組。根據用戶按下的按鈕,富文本框將填充來自陣列中某個字符串的文本。數組未正確初始化,所有記錄都爲空。 c#

以下是正在設置的數組代碼。請注意,有更多的if/else語句做同樣的事情,但有不同的文件,但我不想浪費空間在這裏夾住這些。

namespace ModNote 
    { 
public class backgroundProgram 
{ 
    public static int moduleNumber; 
    public static string[] noteArray; 
    public static string[] moduleArray = new string[7]; 
    const string dataPath = @"Data\"; 
    const string gamesPath = "CGP1005M.txt"; 
    const string algorithmsPath = "CMP1124M.txt"; 
    const string programmingPath = "CMP1127M.txt"; 
    const string operatingPath = "CMP1005M.txt"; 
    const string computerPath = "CMP1125M.txt"; 
    const string webPath = "CMP1129M.txt"; 
    const string informationPath = "CMP1123M.txt"; 


    public backgroundProgram() 
    { 
     #region // Reading to the array 
     // Check the month file exists? 
     if (File.Exists(dataPath + gamesPath)) 
     { 
      // Read in the month file. 
      moduleArray[0] = File.ReadAllText(dataPath + gamesPath); 
     } 
     else 
    } 
} 

不幸的是,數組是空當它涉及到分配他們,所以我把一個斷點在上面的代碼之間,兩條線從未得到執行,我要去哪裏錯了?

請注意,根據用戶在程序開始點擊的按鈕,'backgroundProgram.moduleNumber'的值將會改變。

namespace ModNote 
{ 
public partial class moduleinfoScreen : Form 
{ 
    public moduleinfoScreen() 
    { 
     InitializeComponent(); 
     Shown += moduleinfoScreen_Shown; 
    } 
    public void moduleinfoScreen_Shown(Object sender, EventArgs e) 
    { 
     switch (backgroundProgram.moduleNumber) 
     { 
      case 1: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 2: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 3: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 4: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 5: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 6: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      case 7: 
       moduleinfoTextbox.Text = backgroundProgram.moduleArray[0]; 
       break; 
      default: 
       MessageBox.Show("Nope"); 
       break; 
     } 
    } 

任何想法爲什麼我的數組代碼沒有得到執行?

回答

0

問題是最有可能與這條線:

   if (File.Exists(dataPath + gamesPath)) 

即尋找在數據\ CGP1005M.txt的文件。由於沒有開始的斜槓,它會查找當前路徑中的文件。運行c#程序時,當前路徑將是bin文件夾。

例如,如果您正在運行的項目位於c:\ project,它將嘗試查找c:\ project \ bin \ debug \ data \ CGP1005M.txt中的文件。

覈算項目的位置是否正確?

+0

Thankyou,如果我在名爲「Data」的解決方案資源管理器窗格中包含我所有的GGP ******文件中的文件夾,我將如何鏈接到這些文件? – Durell

+0

啊,好的。進入每個文件的屬性,並確保「複製到輸出目錄」設置爲「始終複製」或「如果更新則複製」。 –

+0

哇我關閉並打開我的文件,顯然它不能打開了,我可能剛剛失去了我的整個項目 – Durell