Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question: Reading Information From A Text File

Status
Not open for further replies.
Jul 19, 2005
5
US
Greetings Everyone,

I'm a former VB6 user who has recently switched to VS2008 (focusing on VB2008). I'm hoping someone can point me in the right direction on this question, and hopefully it won't bring up the ever present XML vs. INI file debate which I have seen some people get very heated over in other boards.

Here is the scenario:

Back in VB6 I had a "settings" file that I used to store information in, which my vb6 program would call on at various points. Examples of what I used to store in the file were things like:

[dblocation]
dblocaiton= <path to database here>

[reportlocation]
rptlocation= <path to report application here>

You get the gist...

I'm looking to recreate this again in VB2008, but without it being in any special format/programming (the INI/XML debate). The program is being used in an environment where I don't need to be worried about the staff members seeing the contents of the file, but I do need a way to be able to open the file and change the information on the fly. When I go between locations, one office may have the program on the X drive on their server, and the other office may have it on drive z, so I need to update my laptop to pull the correct locations of the database.

So to recap - can anyone point me in the right direction of being able to do this:

Create a "text file" that I can store information in such as database locations, which my vb2008 app can read to run.

Can anyone lend a hand?

Thanks in advance for any help!

Rich
 
You should just use the App.Config file which is the standard. You can edit it outside of Visual Studio once deployed. But while developing, create your "settings" by right-clicking on the project in the solution explorer, and choose properties. You should be presented with a screen with tabs. Choose the "Settings" tab and define your settings. You can then reference them in your code like My.Settings.NameOfSettingYouCreated
 
Read (into collection)
Code:
    Public Function ReadFile(ByVal FileLocation As String) As Collection
        Dim sr As StreamReader
        Dim LineList As New Collection

        If File.Exists(FileLocation) = True Then
            sr = New StreamReader(FileLocation)

            Do While Not sr.EndOfStream
                Dim Line As String = sr.ReadLine
                LineList.Add(Line)
            Loop
            sr.Close()
        End If

        Return LineList
    End Function

Write (from collection)
Code:
    Public Sub WriteFile(ByRef FileData As Collection, ByVal FileLocation As String)
        Dim sw As StreamWriter
        Dim obj As New Object

        sw = New StreamWriter(FileLocation) 'Use if always want to create new/overwrite
        'sw = File.AppendText(FileLocation) 'Use if you want to create new/append instead


        For Each obj In FileData
            sw.WriteLine(obj)
        Next

        sw.Close()
    End Sub
You might want to go ahead and parse it here instead of just adding it to a collection, but you should get the gist. The stream reader/writer is the important part. Likely you can just plug in with only a few modifications what you used in VB6.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top