Solved

VB.Net ini file lookup from code

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now