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.

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
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
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
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
I dont think StreamReader can be used in 2003.
Alex Dresko
Svennis
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
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
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 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
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
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.