File I/O to read a specific lineok wait, I've been playing around with the code, but stupid me, I didnt catch this before,

Hey guys,

In need of help. I need to read a line from a .ini file (specifically the 4th line). What it is, is a path to a database which I want to implement as the connection file in my vb window forum --> crystal reports so crystal viewer can pull the data from that database.

[CONFIG]
ReportDirectory=E:\appdata\
MainframePortNumber=1111
ClientsDB=E:\appdata\clients.mdb
ProviderDB=E:\appdata\provider.mdb


So I need VB to read the 4th line and it has to pull E:\appdata\clients.mdb and set it as the database path for crystal reports.

Can anyone shed some light :) thanks.



Answer this question

File I/O to read a specific lineok wait, I've been playing around with the code, but stupid me, I didnt catch this before,

  • unixfox

    Since the file is a standard INI file (it appears to be), rather than reading it as a file, use the Win32 API to read and write the INI settings (Get: then, it doesn't matter what changes are made to the INI file. I Don't have .NET code to do this, yet (only in VB6, currently), as I haven't needed it. I wouldn't anticipate any issues, though.

    The APIs are called GetPrivateProfileString and WritePrivateProfileString, if you want to look them up.



  • NZSnowman

    ok wait, I've been playing around with the code, but stupid me, I didnt catch this before, how is the code advancing   I mean hows the program going to know which line is line 4.  all the counter is doing is advancing but the cursor (in the file) is still at line 1.  So when i print the result I get the first line.

    Here is the code im using

    [code]
            Dim reader As StreamReader = New StreamReader("c:\C4\CONTACT.ini")
            Dim writer As StreamWriter = New StreamWriter("c:\test.txt")
            Dim Path As String = Nothing
            Dim lineCounter As Integer = 0
            While (Path) Is Nothing
                lineCounter = lineCounter + 1
                If lineCounter = 4 Then
                    Path = reader.ReadLine()
                    writer.WriteLine(Path)
                End If
            End While
            reader.Close()
            writer.Close()
        End Sub
    [/code]


  • Russell Mason

    Hey guys, thanks for everything. all worked out good! Final code is below with the splitting characters. I'm pretty sure you can combine path and path2 togheter but I like splitting it for ease of reading.

    Dim reader As StreamReader = New StreamReader("c:\Contact4\CONTACT.ini")
    Dim writer As StreamWriter = New StreamWriter("c:\test.txt")
    Dim Path As String = Nothing
    Dim Path2() As String
    Dim Buffer As String = ""
    Dim lineCounter As Integer = 0
    While (Path) Is Nothing
    Buffer = reader.ReadLine()
    lineCounter = lineCounter + 1
    If lineCounter = 3 Then
    Path = reader.ReadLine()
    Path2 = Strings.Split(Path, "=")
    writer.WriteLine(Path2(1))
    End If
    End While
    reader.Close()
    writer.Close()


  • user33

    StreamReader can be used in 2003.


  • linch12

    In the example code I provided, the Debug.WriteLine part, that will output only the characters after the first = in the string. Thus removing your ClientDB=.

    Good Luck,

    -Joe



  • robert1024

    oh sorry, I forgot to mention im using Visual Studio .NET 2003 not 2005.

    I dont think StreamReader can be used in 2003.


  • Alex Dresko

    You must import System.IO, i think you forgot to do that. Let us know if it worked out for you!


  • Svennis

    ^thanks man, that did the trick.

    But ran into a small problem, got the following error:

    " 'Is' requires operands that have reference types, but this operand has the value type 'Boolean'."

    I'm getting that error for the following line:

    While Not (line = reader.ReadLine()) Is Nothing

    And also do you know how I can chop the first part "ClientsDB=" and only get the path   Im sorry, I'm really new to VB, last time I programmed in VB was oh about 6 years ago.  I'm currently doing C and C++.

    Thanks for all the help! you guys are awesome.


  • chiang

    As a answer on you question how to read the 4th line:


    Dim reader As StreamReader = New StreamReader("c:\myinifile.ini")

    Dim line As String = Nothing
    Dim lineCounter As Integer = 0
    While Not(line = reader.ReadLine()) Is Nothing
    lineCounter = lineCounter + 1

    If lineCounter = 4 Then
    ' TODO: Handle the data from line 4.
    End If
    End While

    '----------------------------------------------------------------
    ' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
    ' Developed by: Kamal Patel (http://www.KamalPatel.net)
    '----------------------------------------------------------------




    But you can use a Ini helper class, that makes live easy to work with INI file. You can find one on codeproject, here.


  • Tùng

    THANK YOU! that did it. Well I took the buffer part and it worked. Now I just have to truncate which I believe I can use String.Split or Readline.Split("=").

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
    Dim reader As StreamReader = New StreamReader("c:\Contact4\CONTACT.ini")
    Dim writer As StreamWriter = New StreamWriter("c:\test.txt")
    Dim Path As String = Nothing
    Dim Buffer As String = ""
    Dim lineCounter As Integer = 0
    While (Path) Is Nothing
    Buffer = reader.ReadLine()
    lineCounter = lineCounter + 1
    If lineCounter = 3 Then
    Path = reader.ReadLine()
    writer.WriteLine(Path)
    End If
    End While
    reader.Close()
    writer.Close()
    End Sub


  • the_bee_keeper

    Dim rdr As New System.IO.StreamReader("C:\MyFile.txt")
    Dim buffer As String = ""
    Dim lineNumber As Integer = 0

    While Not rdr.EndOfStream

        buffer = rdr.ReadLine()
        lineNumber += 1

        If lineNumber = 4 Then
            Debug.WriteLine(buffer.Substring(buffer.IndexOf("=") + 1))
            Exit While
        End If

    End While
    rdr.Close()


  • smanche

    and maybe somone here can help me with another problem aswell...

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=253718&SiteID=1

    thanks


  • Tobi123

    Glad you got it working. You should also be aware that this solution is not flexible at all. Should your ini file change in the future, you have hard coded in the location of that particular item, thus breaking your code. A better solution would be to abandon the ini file in favor of an xml file that is stored in the application data directory.

    -Joe



  • vondueck

    Problem with that is I cannot change the other application. The ini file is a configuration file for another application and the installation is universal and I have no control over so I cannot add an xml file into the installer of that program. (its a program used by the whole province).

    And my reporting application will have to use the contact.ini file and only that. so its not too bad as the default install path is the same and the construction of the ini file is same on every machine its installed on.


  • File I/O to read a specific lineok wait, I've been playing around with the code, but stupid me, I didnt catch this before,