1

I have seen many examples that show StreamReader taking the arugment "Properties.Resources.someTextFile, and apparently they work great. However, in .net 4.5 I still get:

Error Message

//while not end of file, read lines of file and split into array
string myFile = Properties.Resources.data;

string line;

StreamReader reader = new StreamReader(myFile);

while ((line = reader.ReadLine()) != null)
{

    string[] array = line.Split('#');

    string tickerSymbol = array[0];

    string regPattern = array[1];

....

Resources

So, what am I doing wrong?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
daChizzle
  • 231
  • 2
  • 6

1 Answers1

2

By accessing the data text file resource through Properties.Resources the resulting variable myFile should contain the string contents of the file.

string myFile = Properties.Resources.data;

var lines = myFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

foreach(var line in lines)
{
     // Process each line...
}
TMS
  • 126
  • 4
  • Thanks, @TMS. The code you presented accesses the file. Still, I do not understand why the argument "myFile" throws an exception in StreamReader. – daChizzle May 12 '15 at 17:56
  • The StreamReader constructor accepts either a path to a file (c:\directory\filename.ext) or an existing stream. Since Properties.Resources.data returns the contents of the file as a string the reader doesn't know what to do with it. – TMS May 12 '15 at 18:06