Link to home
Start Free TrialLog in
Avatar of doctech
doctech

asked on

VB.Net ini file lookup from code

Hi,

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
Avatar of e_murf1
e_murf1

Hi,
  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" ?>
<configuration>
     <appSettings>
          <add key="PATH" value="\\server\share\whatever"/>
     </appSettings>      
</configuration>

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


Avatar of doctech

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of e_murf1
e_murf1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Made a complete mess of that. If you add a file called app.ini in the program folder with cotents like

PATH=TEST
PATH1=TEST1

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
                SR.Close()
                Return linearray(1)
            End If
            line = SR.ReadLine
        End While
        SR.Close()
        Return "error"
    End Function

You can call the function  with

dim Path as string = ReadFromIni("PATH")
msgbox(Path)

Hopefully there are no mistakes in that.
Eoghan