Home All Groups Group Topic Archive Search About

VB.NET INI or XML file for path locations

Author
19 May 2006 7:22 PM
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.

Author
19 May 2006 7:33 PM
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.
Author
19 May 2006 7:55 PM
Tom Shelton
IL***@NETZERO.NET wrote:
Show quoteHide quote
> 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)

--
Tom Shelton [MVP]
Author
19 May 2006 7:58 PM
Tom Shelton
Tom Shelton wrote:
Show quoteHide quote
> IL***@NETZERO.NET wrote:
> > 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)

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)

--
Tom Shelton [MVP]
Author
19 May 2006 8:59 PM
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.
Author
20 May 2006 12:42 AM
Tom Shelton
IL***@NETZERO.NET wrote:
Show quoteHide quote
> 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>
>
>

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...

--
Tom Shelton [MVP]
Author
22 May 2006 2:38 PM
ILCSP
Thanks a lot Tom, that did the trick.

JR
Author
19 May 2006 7:58 PM
Mike Lowery
INI files are outdated.  XML is the way to go.  VS2005 (not sure about 2003) has
a Settings tab for your Application that creates a file named app.config
containing all the settings you wish to store.  You can edit this file manually
or programmatically after the fact.

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx.

<IL***@NETZERO.NET> wrote in message
Show quoteHide quote
news:1148066571.792077.57050@u72g2000cwu.googlegroups.com...
> 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.
>
Author
20 May 2006 3:44 AM
vbnetdev
http://www.kjmsolutions.com/xmlsettings.htm

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com



<IL***@NETZERO.NET> wrote in message
Show quote Hide quote
news:1148066571.792077.57050@u72g2000cwu.googlegroups.com...
> 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.
>