VB.NET INI or XML file for path locations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ILCSP@NETZERO.NET

    #1

    VB.NET INI or XML file for path locations

    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:\OrdersI n\"
    ExportDir = "C:\OrdersO ut\"
    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.

  • zacks@construction-imaging.com

    #2
    Re: VB.NET INI or XML file for path locations

    I would recommend XML as it is the newer technology and I think
    XMLSerializatio n 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 XMLSerializatio n.

    Comment

    • Tom Shelton

      #3
      Re: VB.NET INI or XML file for path locations


      ILCSP@NETZERO.N ET wrote:[color=blue]
      > 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:\OrdersI n\"
      > ExportDir = "C:\OrdersO ut\"
      > 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.[/color]

      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:

      <configuratio n>
      <add key="ImportDir" value="C:\Order sIn\" />
      </configuration>

      Then, in you code:

      Imports System.Configur ation

      ....

      string importPath = CType (ConfigurationS ettings.AppSett ings
      ("ImportDir" ), String)

      --
      Tom Shelton [MVP]

      Comment

      • Tom Shelton

        #4
        Re: VB.NET INI or XML file for path locations


        Tom Shelton wrote:[color=blue]
        > ILCSP@NETZERO.N ET wrote:[color=green]
        > > 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:\OrdersI n\"
        > > ExportDir = "C:\OrdersO ut\"
        > > 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.[/color]
        >
        > 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:
        >
        > <configuratio n>
        > <add key="ImportDir" value="C:\Order sIn\" />
        > </configuration>
        >
        > Then, in you code:
        >
        > Imports System.Configur ation
        >
        > ...
        >
        > string importPath = CType (ConfigurationS ettings.AppSett ings
        > ("ImportDir" ), String)[/color]

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

        Dim importPath As String = CType (ConfigurationS ettings.AppSett ings
        ("ImportDir" ), String)

        --
        Tom Shelton [MVP]

        Comment

        • Mike Lowery

          #5
          Re: VB.NET INI or XML file for path locations

          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 programmaticall y after the fact.

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

          <ILCSP@NETZERO. NET> wrote in message
          news:1148066571 .792077.57050@u 72g2000cwu.goog legroups.com...[color=blue]
          > 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:\OrdersI n\"
          > ExportDir = "C:\OrdersO ut\"
          > 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.
          >[/color]


          Comment

          • ILCSP@NETZERO.NET

            #6
            Re: VB.NET INI or XML file for path locations

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

            I created a new App.config file by going to "Project" > "Add New Item"[color=blue]
            > "Applicatio n Configuration Settings" in the menu bar.[/color]

            This is what I have in the App.config file

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


            I added the "Imports System.Configur ation " 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(Configura tionSettings.Ap pSettings("Impo rtDir"), String)

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


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

            and adding a extra .getkey in the import path declaration:
            Dim ImportPath As String =
            CType(Configura tionSettings.Ap pSettings.GetKe y("ImportDir" ), String)

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

            Thanks again for your help.

            Comment

            • Tom Shelton

              #7
              Re: VB.NET INI or XML file for path locations


              ILCSP@NETZERO.N ET wrote:[color=blue]
              > Hi guys. Thanks for replying. I'm trying Tom's solution first, but I
              > am getting an error :
              > "Unrecogniz ed configuration section add"
              >
              > I created a new App.config file by going to "Project" > "Add New Item"[color=green]
              > > "Applicatio n Configuration Settings" in the menu bar.[/color]
              >
              > This is what I have in the App.config file
              >
              > <?xml version="1.0" encoding="utf-8" ?>
              > <configuratio n>
              > <add key="ImportDir" value="C:\Order sIn\" />
              > </configuration>
              >
              >[/color]

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

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

              Sorry...

              --
              Tom Shelton [MVP]

              Comment

              • vbnetdev

                #8
                Re: VB.NET INI or XML file for path locations



                --
                Get a powerful web, database, application, and email hosting with KJM
                Solutions




                <ILCSP@NETZERO. NET> wrote in message
                news:1148066571 .792077.57050@u 72g2000cwu.goog legroups.com...[color=blue]
                > 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:\OrdersI n\"
                > ExportDir = "C:\OrdersO ut\"
                > 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.
                >[/color]


                Comment

                • ILCSP@NETZERO.NET

                  #9
                  Re: VB.NET INI or XML file for path locations

                  Thanks a lot Tom, that did the trick.

                  JR

                  Comment

                  Working...