从文本文件资源中读取字符串

本文关键字:读取 字符串 资源 文本 文件 | 更新日期: 2023-09-27 18:02:17

我已经添加了3个文本文件资源到一个应用程序,我试图从它们中读取,但我似乎无法破解它。我尝试过使用文件流,刚刚尝试过使用ResourceReader,我尝试过两者的组合,但没有运气,任何关于我如何开始使用这个的想法?

哦,是的,资源文件的目的是将值加载到form_load的组合框中。我决定这样做,这样欧盟就可以在他/她认为合适的时候增加和删除vals。

如果你认为有更好的(但仍然不引人注目的)方法,那么请分享。

这是我尝试过的,失败了:

Filestream方法,其中TextFile1(to 3).txt是资源文本文件,它在新的Filestream()语句中安静地结束,没有抛出异常

    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());
            }
        }
    }

然后是ResourceReader + FileStream方法,同样的问题,主要区别在于我只是在非fs方法中调用文件名字符串,而不是打开流:

               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();
        }
    }

从文本文件资源中读取字符串

你可以像这样把你想要存储的信息放在app.config文件中。如果您在解决方案资源管理器中右键单击项目并转到设置选项卡,则设置非常容易。

用户可以直接编辑app.config文件,但你也可以给用户一个表单来编辑它。

<?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:如果不明显,这些是System.Collections.Specialized.StringCollection类型设置。

        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;
    }
编辑:

就像我在顶部所说的,右键单击解决方案资源管理器中的项目,然后转到设置选项卡。一旦你在那里:

  1. 创建一个名称为"comboBox1"的设置例子。
  2. 将其类型更改为System.Collections.Specialized.StringCollection
  3. 将Scope更改为您喜欢的任何内容。(如果设置适用于给定用户或整个应用程序,您可以使用此设置)
  4. 在值编辑器中单击,然后单击省略号[…]
  5. 在每一行添加一个你想要的选项。
  6. 根据需要重复

Visual studio将负责如何在配置文件中格式化它,并设置它需要工作的一切

正确,使用TInifile代替。当section不存在时,在第一次执行时,写入默认值。下次直接读这个文件就行了

一个inifile文件应该很容易为EU编辑