Solved

VB.Net ini file lookup from code

Posted on 2007-04-03
5
369 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 62
Access/Visual Basic Question 3 43
Telerik RadEditor Control Save 8 15
Stop Git from being my repository 1 26
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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