0

I have a text file as a resource that I want to read into an array.

 private void button1_Click(object sender, EventArgs e)
    {
        string[] questions = new string[4];
        StreamReader sr = new StreamReader(Properties.Resources.TextFile1);


        for(int i = 0; i < 4; i++)
        {
            questions[i] = sr.ReadLine();
        }
        sr.Close();

        for (int n = 0; n < 4; n++)
        {
           textBox1.Text = questions[n];
        }

I then attempted the following code but keep getting a Null exception

        string[] questions = new string[4];
        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = Properties.Resources.TextFile1;

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream))
Sam
  • 3,320
  • 1
  • 17
  • 22
  • Brilliant Steve thanks for being so helpful, maybe a link to provide some support instead of flagging the question. I cant find the an answer that solves my issue. – Chris Devine Jan 09 '16 at 12:04
  • Sorry but you talk about a resource file so it seem obvious that you should use the [GetResourceManifestStream](https://msdn.microsoft.com/en-us/library/xc4235zt(v=vs.110).aspx) not a StreamReader. If this doesn't work could you explain better your question? I will reopen it. – Steve Jan 09 '16 at 12:09
  • well its obvious to an experienced user, I am attempting to learn and have exhausted a lot of time on youtube videos and other questions on here and still cant find a solution so in a final attempt I asked a question.... – Chris Devine Jan 09 '16 at 12:16
  • Did you set the property "Build Action" for the file to "Embedded resource"?, missing that there is no resource data to work with – Steve Jan 09 '16 at 12:26
  • yes I have it still comes up with a Null error – Chris Devine Jan 09 '16 at 12:29
  • Tried with a simple project. After the call to Properties.Resource.TextFile1 the variable resourceName already contains the contents of a textfile. So you don't need at all the reading from the stream. – Steve Jan 09 '16 at 12:31
  • Firstly thank you for taking time to respond to me, secondly Im still lost as to how to fix this. – Chris Devine Jan 09 '16 at 12:35
  • Where do you get the null reference exception? If you step through your code, does `Properties.Resources.TextFile` contain the contents of the text file? – Sam Jan 09 '16 at 12:37
  • error is at this line using (StreamReader reader = new StreamReader(stream)) and the text in the file appears when I move my mouse over the properties.resources.Textfile line – Chris Devine Jan 09 '16 at 12:39
  • What do you mean by repeating **internal** everywhere? – quetzalcoatl Jan 09 '16 at 12:54
  • Does this answer your question? [Read embedded text file as string array (C#)](https://stackoverflow.com/questions/13342988/read-embedded-text-file-as-string-array-c) – StayOnTarget Jun 26 '20 at 15:38

1 Answers1

3

Since Properties.Resources.TextFile1 contains the contents of the file, you don't need to use a StreamReader at all. You can just parse the string however you like. In your case you could split the string on the newline character:

var questions = Properties.Resources.TextFile1.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Sam
  • 3,320
  • 1
  • 17
  • 22
  • Thanks, I tried that and I get an overload message. the best overloaded match has some invalid arguements – Chris Devine Jan 09 '16 at 12:51
  • Did you get it working? Was that compile error on the `Split` call? Which arguments were invalid? – Sam Aug 11 '16 at 01:33