Question Relative path and accessing text files

Jerome_C

Member
Joined
May 1, 2011
Messages
5
Programming Experience
Beginner
I have created a sub folder (named 'Data') under my project through the solution explorer. I also have created a text file (data.txt) inside the new sub folder. This text file is used to store information such as settings and connections.

I can check the file using the full path. However, I want to avoid errors such as moving the project folder to a different location. Is there anyway to check using a relative path? File.Exists("~\Data\data.txt")

Or is it better to create it under Application.StartupPath?


Thank you in advance
 
Is there any specific reason you are using a text file for these purposes rather than using Settings or Application Configuration Files?

Relative paths will point to the working folder of the executable. Either should work because when you build the .exe and text file should be in the same directory.
 
I'm practicing vb and database. The text file is used to store information such as the address of the server, the name of the database and the login informations required. When the program starts the user will have an option on which connection they would like to access. They can also create new connections.

Let us say:
- that my project is created under C:\VBProjects\Store_app
- the full path of the text file is C:\VBProjects\Store_app\Store_app\Data\data.txt

The Application.StartupPath() points to the C:\VBProjects\Store_app\Store_app\bin\Debug and File.Exists("~\Data\data.txt") does not work either. How do I go about checking relative paths? If Application Configuration Files are better, would you mind helping me on how to use it?



Jerome
 
Oh my, I would like to apologize. I just realized that I posted on the wrong section. This was supposed to be under the vb .net section, not in the vs .net section.



Jerome
 
It is my easiery to use application configuration or settings IMO. I lean towards application configuration.

1. Right click on your project and add new item.
2. Add application configuration file.
3. Then add the following code to the top section of the app.config file:

VB.NET:
<configuration>
  <appSettings>
    <add key="OutputLocation" value ="c:\"/>
  </appSettings>

(Not sure if this next step is always necessary)
4. Add reference to System.Configuration in your project.
5. Then reference the XML configuration via:

VB.NET:
Imports System.Configuration

VB.NET:
     Dim app As New AppSettingsReader()
     outputFolder = app.GetValue("OutputLocation", GetType(String))

Application configuration is used for things you wish to be able to change without having to rebuild the executable. I believe settings you have to rebuild, but like I said I have always just used application configuration.

And as for the section of the forum this belongs in the VS.NET section because this stuff should apply to C# as well.
 
And as for the section of the forum this belongs in the VS.NET section because this stuff should apply to C# as well.
No, All forums here at VB.Net Forums is for VB.Net development related matters only.
VS General means "Visual Studio General" forum, mostly a place where posts are moved to a better suited forum when it is clear what is asked about.
 
I was messing around with the application configuration and you are right, it is easier to use. However, I still don't know how to access the text file inside the sub folder. Also, how can you save multiple settings using the application configuration? This is what my form looks like

capturefn.png



No, All forums here at VB.Net Forums is for VB.Net development related matters only.
VS General means "Visual Studio General" forum, mostly a place where posts are moved to a better suited forum when it is clear what is asked about.

Am I in the wrong section?
 
Personally I see nothing wrong in using text files (use an .ini file extension by convention) for this sort of thing, as long as you encrypt any passwords. I'd put the ini file in the same folder as the exe (which may mean having two copies if you use different output folders for your application in development, for debug and release, but that's not a huge issue really), but you could put it in a subfolder if you want, then use the Application.StartupPath method to reference it.
 
fredX said:
Personally I see nothing wrong in using text files

You are probably correct, it would work using .INI files, just that is an obsolete way to do things. You could also use the API calls to read and write private profile strings to INI files. However, application configuration or application settings would be the way to go IMO depending on what you are specifically using it to do.
 
The way that I do this is save the text file in the root of your solution folder
2011-05-13_0839.png
make sure to copy it to the output folder while debugging
2011-05-13_0841.png

Then you can always find the file by using the Application.Info
Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText(My.Application.Info.DirectoryPath & "/data.txt")
        RichTextBox1.Text = fileContents
 
JeromeC said:
Also, how can you save multiple settings using the application configuration?

Read/Write App.Config File with .NET 2.0 - CodeProject

This is a link to some C# Code that writes to the application configuration file. This can be done very simply through the AppSettings.Add(key, value) function:

Here is some code I came up with which may have to be modified a bit to get your exact desired results:

Imports System.Configuration


        Dim app As New AppSettingsReader
        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        config.AppSettings.Settings.Add("TestKey", "TestVal")
        config.Save(ConfigurationSaveMode.Full, True)
 
Back
Top