Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.")
        Else
            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
add comment

2 Answers

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:

C:\Users\Me>D:\TestPaths\bin\Debug\TestPaths.exe
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
add comment

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

http://stackoverflow.com/questions/5745911/relative-path-names-vb2010

This seems to have done the trick for now.

share|improve this answer
add comment

Your Answer

 
discard

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.