2016-04-20 61 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