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

C# 从文本文件资源中读取字符串,c#,resources,filestreams,C#,Resources,Filestreams,我在一个应用程序中添加了3个文本文件资源,并试图从中读取内容,但我似乎无法破解它。我试过使用filestream,也试过使用ResourceReader,我试过两种方法的组合,但运气不好,关于如何开始使用它,有什么想法吗 哦,是的,资源文件的目的是将值加载到表单加载的组合框中。我已经决定这样做,以便欧盟可以在他/她认为合适的情况下添加和删除VAL 如果你认为有更好(但仍然不引人注目)的方法,那么请分享 以下是我尝试过但失败的地方: Filestream方法,其中TextFile1(to 3).t

我在一个应用程序中添加了3个文本文件资源,并试图从中读取内容,但我似乎无法破解它。我试过使用filestream,也试过使用ResourceReader,我试过两种方法的组合,但运气不好,关于如何开始使用它,有什么想法吗

哦,是的,资源文件的目的是将值加载到表单加载的组合框中。我已经决定这样做,以便欧盟可以在他/她认为合适的情况下添加和删除VAL

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

以下是我尝试过但失败的地方:

Filestream方法,其中TextFile1(to 3).txt是资源文本文件,它在newfilestream()语句中安静地消失,没有引发异常

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

确切地说,使用TInifile代替。 当节不存在时,在第一次执行时,写入默认值。 下次只需读取文件即可


对于EU,INI文件应该很容易编辑,您只需将要存储的信息像这样放置在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>
编辑: 正如我在顶部所说,在解决方案资源管理器中右键单击该项目,然后转到“设置”选项卡。一旦你在那里:

  • 创建一个任意名称的设置,“comboBox1”用于 例如
  • 将其类型更改为System.Collections.Specialized.StringCollection
  • 将范围更改为您喜欢的任何范围。(如果设置适用于给定用户或整个应用程序,您可以使用此选项进行设置)
  • 在值编辑器中单击,然后单击行右侧的省略号[…]按钮
  • 在每行上添加所需的选项
  • 根据需要重复

  • Visual studio将负责如何在配置文件中设置所有内容的格式,并设置其工作所需的所有内容。

    在“设置”部分将这些内容添加到应用程序的配置文件中,而不是滚动自己的配置文件,这不是更好吗?是的,我想,但是用户如何编辑这些值呢?对不起,但你的问题是什么?问题是我不知道怎么做,我想从SOI的资深成员那里得到一些建议。你需要3个文件,这是基于架构的吗,或者仅仅是您的实现选择?找到了一些关于这方面的信息,是否涉及到使用指令?还有31条消息说(属性+元素x没有模式信息),其中x几乎是所有的东西不要试图直接复制我放在那里的东西。如果你自己做的话会更有意义。我将编辑我的答案,为您提供有关添加项目的进一步说明。
            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;
        }