Solved

VB.Net ini file lookup from code

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
Help with error when uploading excel file 3 36
VB.NET - Error - Object not set in Module 4 39
Visual Studios 1 76
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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