0

I want to open a Resource File with the FileStream Class. It's a text File and I want to read it Line by Line.

FileStream fs = new FileStream(Properties.Resources.Testing, FileMode.Open, FileAccess.Read);

The called Exception is System.ArgumentException and it says there is an invalid character.

I hope anyone can help me to fix this, or if theres a better way it's also ok but I need the File in the .exe so it needs to be a Resource..

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Criska
  • 89
  • 1
  • 8
  • What do you mean with Value? It is a txt file and I have added it via Resources in Visual Studio. – Criska Jan 05 '15 at 23:23
  • have you tried writing some simple code that will open the textfile and read the contents ..? perhaps showing more relevant code would help – MethodMan Jan 05 '15 at 23:24
  • before I can read the file I need to solve this line, or I am wrong? – Criska Jan 05 '15 at 23:25
  • 5
    A `FileStream` is used to operate on files in the file system. An embedded resource is not part of the file system. – David Heffernan Jan 05 '15 at 23:29
  • See this question: http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – David Heffernan Jan 05 '15 at 23:30
  • I already had looked up that question, but it didnt helped a lot because I got a NULL pointer in the Streamreader.. – Criska Jan 05 '15 at 23:37

1 Answers1

2

When you add a text file as a resource, it will be embedded as a string. So your FileStream constructor call will assume that you are trying to open a file on disk with a name that is the same as the text file content. That ends poorly of course.

It isn't very clear if you really want a stream, the string tends to be good as-is, you might consider the String.Split() method to break it up into lines. Or maybe you like the StringReader class so you can use ReadLine():

    using (var rdr = new StringReader(Properties.Resources.Testing)) {
        string line;
        while ((line = rdr.ReadLine()) != null) {
            // Do something with line
            //...
        }
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • If it helps you, in the text file are bytes and I want them in a byte[] beginning from the top to the bottom. through your answer it make sense to me why the debugging showed that the "file" has the /r/n in it.. So I will have a look if there is a "StringReader" or a fitting operator :) – Criska Jan 05 '15 at 23:35
  • Hmm, no, that does not help. A text file contains characters. – Hans Passant Jan 05 '15 at 23:37
  • But I got success. I can acces that String directly, I dont need any Reader or something similar. But actually I'm not sure if it's one Big String or many little bricks – Criska Jan 05 '15 at 23:42
  • @Criska in C# strings are strings. If you read the resource and get the string, then you should have the entire string. it is up to you to parse it as you need it. – psubsee2003 Jan 05 '15 at 23:43
  • Console.WriteLine(line + " -"); With this line, my output looks like 11 - 34 - 55 - and so on, in my Opinion this wouldnt happen if this is a whole string.. All Over I think @HansPassant solved it, the other answers were also great! Thanks – Criska Jan 05 '15 at 23:46