Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Working on something in XNA, need to be able to read in data from a text file for something.

Trying to figure out how I can shorten this pathname down, so instead of it always checking there, it just checks in the directory where the actual program is.

I tried doing "\Data\Zone\"+filenamestring+".txt" as my path but it didnt work.

What I have that works:

Sub loadFromFile(ByVal fileNameString As String)
        If System.IO.File.Exists("C:\Users\user\documents\visual studio 2010\Projects\asdf\asdf\asdf\Data\Zone\" + fileNameString + ".txt") Then
            MsgBox("It found it.")
            MsgBox("Could not find it")
        End If
End Sub

While this works, unfortunately this will probably only work on my machine. How do I shorten the file path so its flexible and relevant to the directory where the program is actually located at?

share|improve this question
up vote 3 down vote accepted

Beware that there are multiple ways of getting the relevant directory:

  1. The Environment.CurrentDirectory(MSDN) gives you the working directory of the app. That is not always what you want. For example, you call the .exe of your game from C:\, and the working directory will be C:\ not the one where the .exe resides
  2. AppDomain.CurrentDomain.BaseDirectory(MSDN) or Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)(MSDN) will give you the executable's directory. That is the directory where the executable resides.

The following is an example:

Environment.CurrentDirectory= C:\Users\Me
AppDomain.CurrentDomain.BaseDirectory= D:\TestPaths\bin\Debug\
Assembly.GetExecutingAssembly().Location= D:\TestPaths\bin\Debug

If you want just to read data that accompaniates the executable (like Content), then I'd suggest using the second approach (executable's directory)

share|improve this answer

I found a solution after a bit of research on Stack Overflow:

This seems to have done the trick for now.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.