I'm making a game and I need to load some text files to create levels. After much frustration in trying to get them to work I placed the files in the debug folder of my project (I think this is a bad idea but I didn't really know how else to get the StreamReader to work).

The problem is that this means I can't get the project to run when I publish it because I don't know what to do with the text files. Where have I gone wrong and what can I do about it?


2 Answers 2


I can't verfiy this right now (no XNA installed), but I'm pretty sure the following should work:

  • Add the text file to your project.
  • Open the file's properties (within VS) and set the following values:
    • Build Action: Content
    • Copy to Output Directory: Copy if newer (or Copy always)

Then open the project properties, select the "Publish" tab and click on "Application Files...". In the new window you should see your text file being listed and you're able to change it's group or status (default one should be fine).

  • \$\begingroup\$ +1. "Content" causes it to be copied into the publish directory on publication. "Copy if newer"/"Copy always" causes it to be copied into the output directory by the build process. These two things are independent, but generally you'll want both. \$\endgroup\$ Jul 11, 2013 at 11:40

The StreamReader looks for the file in the current folder (if you just start your App it will be the folder of your ".exe") and in all $path$ folders (e.g. in the Window folder). If you open a CMD window and enter echo %path%, you will see all of the valid paths.

To access your file at runtime you have to follow the following steps:

1) Get the Application-Path

System.Sting path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

2) Add "\" to the end

if (String.Compare(path.substr(path.lengh-1, 1), "\\") != 0)
    path = path + "\\";

3) Open file relative to the path:

FileStream fileStream = new FileStream(path + "data\\level.txt", FileMode.Open);

If your folder structure looks like this:

\bin\game.exe <- your program

Then you need to go up a folder level, so use use '..'

FileStream fileStream = new FileStream(path + "..\\data\\level.txt", FileMode.Open);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .