【问题标题】:How to get data from text resource file and put in combobox如何从文本资源文件中获取数据并放入组合框
【发布时间】:2019-07-16 10:46:45
【问题描述】:

我在 txt 文件中有很多数据文件,比如这个简短的例子

123456
754124
956412
789654

每个文件可以有几十行。每个文件填充一个单独的组合框。从文件夹中的静态文件中,我可以使它工作

string[] fname = {"fridge", "washer", "freezer", "dishwasher"}; 

 for (int i = 0; i < fname.Length; i++)
 {
      string[] lineOfContents = File.ReadAllLines(@"d:\\temp\\" + fname[i] + ".txt");

      ComboBox cmbobox = (ComboBox)this.Controls["cmbobx_" + fname[i]];

      foreach (var line in lineOfContents)
      {
          string[] data = line.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
          cmbobox.Items.Add(data[0]);
       }
       cmbobox.SelectedIndex = 0;
}

但是当我从嵌入式资源中读取数据时,我需要这样做。我将文本文件拉到 project.properties.resources 中,所以我将它们放在 exe 中。我知道我需要从资源中将其流式传输出来,但随后我迷失了如何将流转换为所有换行符等并将其格式化以将其添加到组合框。

我尝试了很多东西,我认为我得到的最接近的方法如下,尽管它告诉我我什么都没有(NULL)。

string[] fname = {"fridge", "washer", "freezer", "dishwasher"}; 
var assembly = Assembly.GetExecutingAssembly();
for (int i = 0; i < fname.Length; i++)
{
    string lineOfContents;
    string name = fname[i] + ".txt";

    using (Stream resourceStream = assembly.GetManifestResourceStream(name))
    {
       if (resourceStream != null)
       {
          using (StreamReader reader = new StreamReader(resourceStream))
          {
             lineOfContents = reader.ReadToEnd();
           }
        }
      }
      ComboBox cmbobox = (ComboBox)this.Controls["cmbobx_" + fname[i]];
      cmbobox.SelectedIndex = 0;
   }

非常感谢任何有助于将流放入组合框的帮助。


感谢下面的链接,我在搜索时没有得到它,所以谢谢,它有帮助。在我整理之前的工作代码是:

string[] fname = {"fridge", "washer", "freezer", "dishwasher"}; 

 for (int i = 0; i < fname.Length; i++)
 { 
   string resource_data = Properties.Resources.ResourceManager.GetString(fname[i]);

   string[] lineOfContents = resource_data.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

   ComboBox cmbobox = (ComboBox)this.Controls["cmbobx_" + fname[i]];

    foreach (var line in lineOfContents)
    {
        string[] data = line.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
        cmbobox.Items.Add(data[0]);
     }
        cmbobox.SelectedIndex = 0;
}

【问题讨论】:

标签: c# combobox resources


【解决方案1】:

感谢您的链接,我在搜索时没有得到链接,所以谢谢,它有所帮助。在我整理之前的工作代码是:

string[] fname = {"fridge", "washer", "freezer", "dishwasher"}; 

 for (int i = 0; i < fname.Length; i++)
 { 
   string resource_data = Properties.Resources.ResourceManager.GetString(fname[i]);

   string[] lineOfContents = resource_data.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

   ComboBox cmbobox = (ComboBox)this.Controls["cmbobx_" + fname[i]];

    foreach (var line in lineOfContents)
    {
        string[] data = line.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
        cmbobox.Items.Add(data[0]);
     }
        cmbobox.SelectedIndex = 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多