-1

I'm trying file I/O in C# (haven't done so at all before since I'm a beginner). I put a simple text file containing "some text" (literally) in the solution's resources, wrote some code based on online tutorials and pressed F5 to build. I was expecting "some text" to show up in the console window, but an exception did!

Could not find file 'F:\VS\FileTesting\FileTesting\bin\Debug\some text'.

As I said above, "some text" is the content of the file and its name is "important.txt"

Here's the code I'm using:

try
{
     /* using (StreamReader reader = new StreamReader(Properties.Resources.important))
     {
          string line;

          while ((line = reader.ReadLine()) != null)
          {
              Console.WriteLine(line);
          }
     } */
     string contents = File.ReadAllText(Properties.Resources.important);
     Console.WriteLine(contents);
     Console.WriteLine("Contents displayed successfully.");
     Console.ReadKey();
}
catch (Exception e) {
    MessageBox.Show("An error occured. Exception: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

(the commented section above is from another post here, which explained how to read a file line by line)

Based on @JonSkeet's comment below, I think I found the cause: it looks like File.ReadAllText reads and treats the contents of Properties.Resources.important as a filename rather than text as intended. "some text" is the only thing it can find, thus it searches a file with that name in the build directory, which is nonexistent. What I did was rename the original file to "pointer.txt", wrote "important.txt" in it and used it as a pointer to the file I want to read.

I can get away with having it read an external file, but I want the file in my resources. Plus, I can't seem to make it an embedded one - the option is disabled. Does that also have to do anything?

Any help is appreciated.

na-no.
  • 344
  • 1
  • 3
  • 11
  • 1
    Well presumably `Properties.Resources.important` is "some text". I strongly suspect if you look in your resource file, you'll see "some text", but it should be "important.txt". – Jon Skeet Dec 11 '20 at 20:22
  • @JonSkeet Thank you so much! I made up something real quick based on your comment: I renamed the resource file to "pointer.txt" with "important.txt" in it and used it as a pointer. It works now! – na-no. Dec 11 '20 at 20:53
  • 1
    I don't follow what you mean, but at the moment I don't think the question is going to help anyone, and there isn't enough information to really answer it. I would suggest you delete it, or edit it significantly to provide more information. – Jon Skeet Dec 12 '20 at 07:40

1 Answers1

1

You do not need to use File methods to read the contents of a resource. The File methods are for reading files from the file system.

To access the contents of the resource, simply write Properties.Resources.important. You can do Console.WriteLine(Properties.Resources.important) to write the contents of the resource to the console.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • Thanks! I'll try it out later though since I'm away from my home. – na-no. Dec 12 '20 at 12:45
  • Thanks again! I tried it and it works now. You also made the difference between a file in resources and a file in the file system clear to me. – na-no. Dec 12 '20 at 14:14