Solved

VB.Net ini file lookup from code

Posted on 2007-04-03
5
372 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:doctech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 

Expert Comment

by:e_murf1
ID: 18842222
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))


0
 

Author Comment

by:doctech
ID: 18842262
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
0
 

Expert Comment

by:e_murf1
ID: 18842274
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.
0
 

Accepted Solution

by:
e_murf1 earned 50 total points
ID: 18842291
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)
linearray=split(linein,"=")
if linearray(0)=key
return linearray(1)
line=sr.readline
end while
end functoin
0
 

Expert Comment

by:e_murf1
ID: 18842340
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
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question