0

I have many files of data in txt files like this short example

123456
754124
956412
789654

They can have tens of lines in each file. Each file populates a separate combobox. From a static file in a folder I can make it work

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

but I need to do this when I read the data from embedded resources. I pulled the text files into the project.properties.resources so I have them inside the exe. I understand I will need to stream it out from resources but then I get lost in knowing how to convert the stream with all its newlines etc and format it to add it to the combobox.

I tried many things and the closest I think I have got is as follows although it tells me I have hold of nothing (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;
   }

Any help in getting the stream into the combo box would be much appreciated.


Thanks for the link below, I did not get that when I searched so thanks and it helped. The working code before I tidy it up is:

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;
}
user3884423
  • 556
  • 1
  • 5
  • 20
  • 1
    possible duplicate with https://stackoverflow.com/questions/15722455/read-text-file-from-c-sharp-resources ? – Christoph Biegner Jul 16 '19 at 10:56
  • Possible duplicate of [Read text file from C# Resources](https://stackoverflow.com/questions/15722455/read-text-file-from-c-sharp-resources) – Selvin Jul 16 '19 at 11:37

1 Answers1

0

Thanks for the links, I did not get that when I searched so thanks and it helped. The working code before I tidy it up is:

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;
}
user3884423
  • 556
  • 1
  • 5
  • 20