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