VB.NET INI or XML file for path locations


I

ILCSP

Hello, I have a complete project in VB.NET (2003) with all the path to
the folders where the files being used are. For example I have these
variables:

' Setup Directory Paths
ImportDir = "C:\OrdersIn\"
ExportDir = "C:\OrdersOut\"
LogDir = "E:\OrdersLogs\"

This work fine right now. However, if for some reason in the near
future I have to move these folders to another drive, I will have to go
back to the VB.NET code and replace them there. What I would like to
do is create is a INI or XMF file with these paths and then use it in
my project. I'm hoping to also add more variable values such as the
connection string to the SQL database in our server.

Since I am fairly new to VB.NET. I have no idea where to start. How
can I set this paths in the file and how do I open/use the file in the
VB.NET code?

Thanks for your help.
 
Ad

Advertisements

Z

zacks

I would recommend XML as it is the newer technology and I think
XMLSerialization is the best thing since sliced bread! :)

After reading in the XML file with the Deserialization method, the
values for the file paths will be stored in a Class Instance's
Properties.

You could then write a configuration form that allows the user to
change one or more values and write the XML file back out with the
Serialization method.

Check the Help out, search for XMLSerialization.
 
T

Tom Shelton

Hello, I have a complete project in VB.NET (2003) with all the path to
the folders where the files being used are. For example I have these
variables:

' Setup Directory Paths
ImportDir = "C:\OrdersIn\"
ExportDir = "C:\OrdersOut\"
LogDir = "E:\OrdersLogs\"

This work fine right now. However, if for some reason in the near
future I have to move these folders to another drive, I will have to go
back to the VB.NET code and replace them there. What I would like to
do is create is a INI or XMF file with these paths and then use it in
my project. I'm hoping to also add more variable values such as the
connection string to the SQL database in our server.

Since I am fairly new to VB.NET. I have no idea where to start. How
can I set this paths in the file and how do I open/use the file in the
VB.NET code?

Thanks for your help.

You can accomplish this by adding a config file to your application.
Then, you can add these items to it by editing the app.config file:

<configuration>
<add key="ImportDir" value="C:\OrdersIn\" />
</configuration>

Then, in you code:

Imports System.Configuration

....

string importPath = CType (ConfigurationSettings.AppSettings
("ImportDir"), String)
 
T

Tom Shelton

Tom said:
You can accomplish this by adding a config file to your application.
Then, you can add these items to it by editing the app.config file:

<configuration>
<add key="ImportDir" value="C:\OrdersIn\" />
</configuration>

Then, in you code:

Imports System.Configuration

...

string importPath = CType (ConfigurationSettings.AppSettings
("ImportDir"), String)

Crap! The fact that I almost exclusively use C# now is comming out :)
That above line should be:

Dim importPath As String = CType (ConfigurationSettings.AppSettings
("ImportDir"), String)
 
I

ILCSP

Hi guys. Thanks for replying. I'm trying Tom's solution first, but I
am getting an error :
"Unrecognized configuration section add"

I created a new App.config file by going to "Project" > "Add New Item"
"Application Configuration Settings" in the menu bar.

This is what I have in the App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="ImportDir" value="C:\OrdersIn\" />
</configuration>


I added the "Imports System.Configuration " line all the way to the top
of the code next to the other Imports lines.

Then I added this line to my code:
Dim ImportPath As String =
CType(ConfigurationSettings.AppSettings("ImportDir"), String)

I tried to test it by doing a message box pop up.
MsgBox(ImportPath)


I also tried some variations such as changing the add key line in the
app.config to:
<add key="ImportDir" value="C:\OrdersIn\"></add>

and adding a extra .getkey in the import path declaration:
Dim ImportPath As String =
CType(ConfigurationSettings.AppSettings.GetKey("ImportDir"), String)

That did not help either. Do you have any ideas why I am getting this
error?

Thanks again for your help.
 
Ad

Advertisements

T

Tom Shelton

Hi guys. Thanks for replying. I'm trying Tom's solution first, but I
am getting an error :
"Unrecognized configuration section add"

I created a new App.config file by going to "Project" > "Add New Item"

This is what I have in the App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="ImportDir" value="C:\OrdersIn\" />
</configuration>

It's my fault :) It should look like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ImportDir" value="C:\OrdersIn\" />
</appSettings>
</configuration>

Sorry...
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top