2

I have a List<string> that I would like to populate via a text file that is set as a project resource. I have looked all over on a way to do this but haven't yet found one that doesn't cause my program to crash.

If I manually populate the list...

_names.Add("Sam");
_names.Add("John");
_names.Add("Mike");

...everything works. My text file has each name on a separate line, no commas or anything. When I try to read in the names, the program crashes, no matter which route I take. This is the most recent way I've tried, though there are many others:

using (var reader = new StreamReader(Properties.Resources.sampleNamesMale))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        _names.Add(line);
    }
}

Also, I can't isolate the reason for the crash because every time it does, the error just mentions ViewModelLocator, which is entirely irrelevant to this issue.

Does anybody have any ideas about how to fix this? I would certainly appreciate any advice.

Update: Try-catch yields no results. This is the error I get:

XamlParseException occurred - 'The invocation of the constructor on type 'AoW.ViewModels.ViewModelLocator' that matches the specified binding constraints threw an exception.' Line number '13' and line position '10'.

It points at InitializeComponent() in my main window's constructor.

Update 2: The real exception is this:

"ArgumentException occurred - Illegal characters in path." It points at the using (var reader.... line.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
Jason D
  • 2,634
  • 6
  • 33
  • 67
  • 3
    Did you use try catch block and see detailed exception message? – David Jun 11 '13 at 03:56
  • http://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx – Yaugen Vlasau Jun 11 '13 at 04:00
  • I've updated my post. Also, Yaugen, that's one of the methods I've tried. – Jason D Jun 11 '13 at 04:10
  • Try to look at http://stackoverflow.com/questions/9045378/xaml-parse-exception-xmlnsx-http-schemas-microsoft-com-winfx-2006-xaml – Piotr Stapp Jun 11 '13 at 04:13
  • @Garath Thanks so much for that. I can actually see the real exception now. I'm updating my OP. – Jason D Jun 11 '13 at 04:17
  • Out of curiosity can you do this while simply writing to a console window and not adding it to a collection. Or does it work if you simply try to read a file like c:\names.txt into the collection? That might help you isolate the issue. What sort of application is this anyway? – Dan Snell Jun 11 '13 at 04:22
  • Interesting. If I type the path in directly it works. I also noticed that when I try to load the file from Resources, though it is a txt file, it's handled as a string. Is there any way to fix that? – Jason D Jun 11 '13 at 04:36

3 Answers3

5

Use a StringReader instead of a StreamReader:

using (var reader = new StringReader(Properties.Resources.sampleNamesMale))

You are getting that error because StreamReader(string) expects a file path. If you are providing the actual text in Properties.Resources.sampleNamesMale, you have to use a StringReader.

fcuesta
  • 4,429
  • 1
  • 18
  • 13
1

The only way you could get the exception:

ArgumentException occurred - Illegal characters in path.

is if the path returned by Properties.Resources.sampleNamesMale was literally invalid:

using (var reader = new StreamReader(Properties.Resources.sampleNamesMale))
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • You are correct. For some reason 'sampleNamesMale' does not exist, so says the debugger. – Jason D Jun 11 '13 at 04:49
  • @JasonD: that would indicate that the resource isn't setup properly. Just remove it and re-add it via the designer. – Mike Perrenoud Jun 11 '13 at 04:50
  • I did that, same results. What's interesting is that if I create a local variable and assign the resource to it, I get the same error, but in the debugger, it shows that it does have a value. I think the issue may be that VS is treating the txt file as a string. Perhaps if I could find a way to break up the string into the appropriate parts I could just do that. – Jason D Jun 11 '13 at 04:55
  • @JasonD, what's the literal value of the string when you place it in a local variable? – Mike Perrenoud Jun 11 '13 at 05:01
1

After second update the answer is very easy: display in debugger what is a path to your file and make it correct. Probably it contains spaces in the end or not escaped \

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • I'm sorry to be a bother, but I don't know how to do what you are suggesting. Can you elaborate a bit? – Jason D Jun 11 '13 at 04:25
  • When you are debugging set a watch on that particular variable and look at what the actual path resolves as. – Dan Snell Jun 11 '13 at 04:35
  • "The name 'sampleNamesMale' does not exist in the current context" is what it's saying when I put a watch on it in the debugger. I don't know how to get any more data beyond that. – Jason D Jun 11 '13 at 04:42