I have added 3 text file resources to an app and am trying to read from them but I just can't seem to hack it. I've tried using a filestream and have just tried using ResourceReader and I've tried a combination of the 2 but no luck , any ideas on how I could get started with this?
Oh yeah, the purpose of the resource files is to load values into combo boxes on form_load. I've decided to do it like that so the EU can add and remove vals as he/she sees fit.
If you think there are better (but still unobtrusive) ways of doing it then please do share.
Here's what I`ve tried and failed with:
the Filestream approach, where TextFile1(to 3).txt is the resource text file, it dies quietly on the new FileStream() statement, with no exception thrown
private void Scan_Form_Load(object sender, EventArgs e)
{
// read combo box values from textfile
AddVals("TextFile1.txt",cmbBox1);
AddVals("TextFile2.txt", cmbBox2);
AddVals("TextFile3.txt", cmbBox3);
}
private void AddVals(string fileName,ComboBox thisBox)
{
using (FileStream repFs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
StreamReader strReader = new StreamReader(repFs);
ArrayList aVals = new ArrayList();
while (strReader.Peek() != -1)
{
aVals.Add(strReader.ReadLine());
}
foreach (object val in aVals)
{
thisBox.Items.Add(val.ToString());
}
}
}
Then the ResourceReader + FileStream approach, same problem , the main difference being that I just call the filename string in the non-fs approach instead of opening the stream:
private void AddVals(string fileName, ComboBox thisBox)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
IResourceReader reader = new ResourceReader(fs);
IDictionaryEnumerator en = reader.GetEnumerator();
while (en.MoveNext())
{
string val = en.Value.ToString();
thisBox.Items.Add(val);
}
fs.Close();
reader.Close();
}
}