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",cmb开发者_StackOverflowBox1);
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();
}
}
You can just place the info you want to store in the app.config file like so. It's very easy to set up if you just right click on the project in the solution explorer and go to the settings tab.
The user could technically edit the app.config file directly but you could also give the user a form to edit it on.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CSharpWindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<CSharpWindowsFormsApplication1.Properties.Settings>
<setting name="comboBox1" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>choice1</string>
<string>choice2</string>
<string>choice3</string>
<string>choice4</string>
<string>choice5</string>
</ArrayOfString>
</value>
</setting>
<setting name="comboBox2" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>choice1</string>
<string>choice2</string>
<string>choice3</string>
<string>choice4</string>
<string>choice5</string>
</ArrayOfString>
</value>
</setting>
<setting name="comboBox3" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>choice1</string>
<string>choice2</string>
<string>choice3</string>
<string>choice4</string>
<string>choice5</string>
</ArrayOfString>
</value>
</setting>
</CSharpWindowsFormsApplication1.Properties.Settings>
</applicationSettings>
</configuration>
EDIT: In case it's not apparent, those are System.Collections.Specialized.StringCollection type settings.
and...
private void Scan_Form_Load(object sender, EventArgs e)
{
// read combo box values from textfile
comboBox1.DataSource = Properties.Settings.Default.comboBox1;
comboBox2.DataSource = Properties.Settings.Default.comboBox2;
comboBox3.DataSource = Properties.Settings.Default.comboBox3;
}
EDIT: Like I said at the top, right click on the project in the solution explorer and go to the settings tab. Once you're in there:
- Create a setting with whatever name you wish, "comboBox1" for example.
- Change it's type to System.Collections.Specialized.StringCollection
- Change the Scope to be whatever you like. (You can use this to set if the setting applies to a given user or the whole app)
- Click in the value editor and click the elipsis [...] button at the right side of the line.
- Add an option that you want on each line.
- Repeat as needed.
Visual studio will take care of how to format it all in the config file and set up everything it needs to work.
Exactly, use TInifile instead. On first execution when the sections doesn't exist, write default values. Next time(s) just read the inifile.
An inifile should be easy to edit for the EU
精彩评论