Hi

I am wondering how to point to a relative file that would be the same on all computers. How would I go about this.

Many Thanks

Recommended Answers

All 4 Replies

Relative to what? I guess relative to your application. To be more precise, relative to the current directory. Here's the code:

Dim FileName As String

' File is two levels up from the current directory
FileName = "..\..\test.txt"

' Test if the file really exists
If My.Computer.FileSystem.FileExists(FileName) Then
    ' Dump the file content
    TextBox1.Text = My.Computer.FileSystem.ReadAllText(FileName)
Else
    ' Error: file not found
End If

HTH

Thanks Teme64, will try that. So when exported it will be relative to the current directory of the .exe file but while I'm still writing it, it will be relative to the current directory of the Visual Studio solution file?

I've also been told that I could try saving the stuff to the 'app data' folder. Do you know how I could do this?

Once again, thanks for you help.

I've also been told that I could try saving the stuff to the 'app data' folder.

That's a much better idea. Now you can write code that works with every Windows version and the code also works with UAC.
Here's the first snippet:

Dim PathName As String
Dim FileName As String = "test.txt"

' Get the special folder: Common App Data, now every user uses the same directory with this app
PathName = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
' Catenate path and filename
FileName = Path.Combine(PathName, FileName)

' Test if the file really exists
If My.Computer.FileSystem.FileExists(FileName) Then
    ' Dump the file content
    TextBox1.Text = My.Computer.FileSystem.ReadAllText(FileName)
Else
    ' Error: file not found
End If

And here's a bit different snippet:

Dim PathName As String
Dim FileName As String = "test.txt"

' Get the special folder: App Data, now every user uses his/hers private app data directory with this app
PathName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
' Catenate path and filename
FileName = Path.Combine(PathName, FileName)

' Test if the file really exists
If My.Computer.FileSystem.FileExists(FileName) Then
    ' Dump the file content
    TextBox1.Text = My.Computer.FileSystem.ReadAllText(FileName)
Else
    ' Error: file not found
End If

Previous code uses roaming profiles. The third version would be PathName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) . Instead of roaming profiles, Local App Data is for non-roaming users.

The choice is yours but I suggest using first or second example.

HTH

commented: Clear, friendly + helpful +1

Many thanks for this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.