Relative Paths

Invisionsoft

Well-known member
Joined
Apr 20, 2007
Messages
64
Location
United Kingdom
Programming Experience
1-3
Hello,

My program has to store files externally to work with. Before now the path was fixed at C:/DSGameMaker, I did the following:

System.IO.File.ReadAllText("C:/DSGameMaker/data.dat")

However I want to let users install the program wherever they want, so I changed that line and others to:

System.IO.File.ReadAllText("data.dat")

Which works great, until you do this:

System.IO.File.ReadAllText("D:/X/Y.Z")

As soon as I put a fixed path into the ReadAllText function it changes the relative path to D:/X. Which means the following function looks inside D:/X instead of the apps directory:

System.IO.File.ReadAllText("data.dat")

And complains that "D:/X/data.dat" is not found.


How can I use relative paths but also read from full file names, for example from an OpenFileDialog, without VB changing my relative path, and breaking it all from then onwards?


Thank you.
 
With Vista, and I presume Windows 7 will be the same, you can experience permission problems when trying to write to files in the Application Directory.

One way to get round this problem is to let the users install the Application where they like but use the users ApplicationData Directory for the data file.

This can be retrieved by:
VB.NET:
    Dim path As String =   Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
  ' You could also use either of
  ' Environment.SpecialFolder.ApplicationData
  ' Environment.SpecialFolder.CommonApplicationData

But if you really must use the Application Directory look up Application.StartupPath.
 
No can do unfortunately. Typically Microsoft, the path has spaces in, and you can't run MAKE from a path with spaces in.

This is against the point though, since I am debugging the app on XP, and, it's not a permissions issue. VB is simply changing it's relative path location when I really didn't think it would.
 
Ah, you didn't mention MAKE. :)
Haven't used that for a long, long time. Although from memory you could use paths with spaces if you enclose the whole path in quotes.

That's all I can think of.
Sorry not to be more help.
 
I'll go with your original idea of letting the user install the app wherever and have all relative paths using Environment.SpecialFolder.ApplicationData at the start.

To run make I'll just create C:/DSGMTempCompile directory and run from there.Though annoyingly this doesn't solve my problem mainly. Main problem is lots of my users do not have C:/ drive and want to install the app on another drive. How then do I know which drive is the main one, so I can create my temp directory?
 
Lookup GetLogicalDrives (there are two of these, but from memory they do the same thing) and DriveInfo, I'm sure that you can work it out from there. :)
 
Really everything for your app should be stored in the user's Application Data directory, which you can get using:
VB.NET:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.DirectorySeparatorChar)
Be sure to put an "Imports System.IO" statement at the top of the class (the form)
 
Back
Top