• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

VB.Net ini file lookup from code


How do i get VB.Net to look at an ini file for a path.
Basicaly i have a path c:\source and a target c:\target
I want to create 2 ini files/ or registry entries so that the system looks at these and selects the path rather than hard coding the path so users can change the ini file rather than looking at the code.
Also will this work for UNCs
  • 4
1 Solution
  Do you have to use an ini file or can you use a app.config file to store the paths? If you add an app.config file and add the stuff below to the xml you can use the code below that to pull the informaion from the config.

<?xml version="1.0" encoding="utf-8" ?>
          <add key="PATH" value="\\server\share\whatever"/>

Dim appSettings As AppSettingsReader = New AppSettingsReader
Dim Path as string
Path = appsettings.GetValue("PATH", GetType(String))

doctechAuthor Commented:
I am a novice when it comes to VB.net and feel an ini file would be easier to administer whilst i am learning the product. Is it possible with an ini file or a registry entry
I am relatively new to this also but the app.config is very easy to use. You can add it by going to Project-additems. Then add an application configuration file. Add a key and a value for any info you want to put in then use the 3 lines of code to pull it. Apparently this is the way it is meant to be done in .NET.
You could create a file called app.ini in the folder the program is running in with the lines below

Path1=Some path
Path2=Some other Path

then use

dim line as string
dim linearray() as string
Public Function RaedFromIni(Key) as string
 'Dim SR As New StreamReader(Application.StartupPath & "\app.ini")
            'Line = SR.ReadLine
            'While Not (Line Is Nothing)
if linearray(0)=key
return linearray(1)
end while
end functoin
Sorry Made a complete mess of that. If you add a file called app.ini in the program folder with cotents like


Then add this function to your project

    Public Function ReadFromIni(ByVal Key) As String

        Dim SR As New StreamReader(Application.StartupPath & "\app.ini")
        Dim line As String
        Dim linearray() As String
        line = SR.ReadLine
        While Not (line Is Nothing)
            linearray = Split(line, "=")
            If linearray(0) = Key Then
                Return linearray(1)
            End If
            line = SR.ReadLine
        End While
        Return "error"
    End Function

You can call the function  with

dim Path as string = ReadFromIni("PATH")

Hopefully there are no mistakes in that.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now