|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
(newbie) - reading data from a file - in VB 2005 EEless like in the old days when ini files were used. But to be honest, I can't find anything in the documentations (I'm not English, maybe I just don't understand it). Let's assume I have this in the file (called setup.txt): [settings] name1=Vito name2=Doortje txt1=married with children txt2=and still happy pic=both.jpg My program must read this content and assign the data in the rigth hand portion to a variable (declared public)with the name of the left hand portion of each line, and above all the program must search ONLY in the [settings] section ( there will be other additional sections in the future) All tips very welcome, please understand I'm a newbie at programming in VB and I'm not English ... Many thanks Vito Hi Opa,
Do you absolutely have to use that file format? Because if you are free to choose the format of the settings file, you'd be a lot better off using an xml file. If you are stuck using that file format you will need to do the following - I'm not going to come up with the vb.net code for you because i'm too lazy but you will have fun working it out I'm sure :) - Open a Stream object on the file. - Open a StreamReader on the Stream. - Start a loop, calling StreamReader.ReadLine each time. - Continue the loop until the line read was "[settings]" then exit the loop. - Start a new loop, calling StreamReader.ReadLine each time. - Within the loop, get the lefthand side by using string.substring(0, string.indexof("=")) - Do a select case statement on the lefthand side - have case statements for each entry under "[settings]" which do the assigning of the value - Continue the loop until the line read begins with "[". Apologies if I've got anything wrong, I'm just doing this off the top of my head. It should be enough to get you started though, come back if you have any problems :) Phil Opa Vito wrote: Show quoteHide quote > Hello, I need to read data from a file the old fashioned way, more or > less like in the old days when ini files were used. But to be honest, I > can't find anything in the documentations (I'm not English, maybe I just > don't understand it). > > Let's assume I have this in the file (called setup.txt): > > [settings] > name1=Vito > name2=Doortje > txt1=married with children > txt2=and still happy > pic=both.jpg > > My program must read this content and assign the data in the rigth hand > portion to a variable (declared public)with the name of the left hand > portion of each line, and above all the program must search ONLY in the > [settings] section ( there will be other additional sections in the future) > > All tips very welcome, please understand I'm a newbie at programming in > VB and I'm not English ... > > Many thanks > > Vito wingp***@yahoo.com schreef:
> Hi Opa, Hey Phil,> Do you absolutely have to use that file format? Because if you are free I expect a lot of people of our 'secret community' will use this program > to choose the format of the settings file, you'd be a lot better off > using an xml file. if I ever can make it run the way it's intended, and I'm sure it will be much more easy for most of them to edit a simple text file rather than a XML .. Of course this setup file is the smallest of maybe 4 or 5 other files that has to be read the same way ... But I will take a look at XML too. > If you are stuck using that file format you will need to do the <snip>> following - I'm not going to come up with the vb.net code for you > because i'm too lazy but you will have fun working it out I'm sure :) No problem, I believe I understand your explanation, with a bit of search I must find out what code to use. Thanks for your cooperation Vito You need to use the Filestream and streamreader (readline) classes. Then
use string functions to parse each line (using the "=" as the delimiter). Show quoteHide quote "Opa Vito" <gebr***@dit.niet> wrote in message news:wvudnXiusemOLZvYRVnytA@scarlet.biz... > Hello, I need to read data from a file the old fashioned way, more or less > like in the old days when ini files were used. But to be honest, I can't > find anything in the documentations (I'm not English, maybe I just don't > understand it). > > Let's assume I have this in the file (called setup.txt): > > [settings] > name1=Vito > name2=Doortje > txt1=married with children > txt2=and still happy > pic=both.jpg > > My program must read this content and assign the data in the rigth hand > portion to a variable (declared public)with the name of the left hand > portion of each line, and above all the program must search ONLY in the > [settings] section ( there will be other additional sections in the > future) > > All tips very welcome, please understand I'm a newbie at programming in VB > and I'm not English ... > > Many thanks > > Vito > Hi, I'm stuck :-)
This the class I use to read the data: Imports System Imports System.IO Class Class1 Public Shared Sub ReadGeneral() Try Using sr As StreamReader = New StreamReader("c:\general.txt") Dim line As String Dim element As String Do line = sr.ReadLine() Loop Until line = "[general]" Do line = sr.ReadLine() element = line.Substring(0, line.IndexOf("=")) Form1.TextBox1.Text = Form1.TextBox1.Text & element_ & vbCrLf Loop Until line Is Nothing sr.Close() End Using Catch E As Exception Form1.TextBox1.Text = E.Message End Try End Sub End Class Of course I first write to the textbox to see if everything is OK .. but I have an error report saying: "object reference not set to an instance of an object" It's the "element = line.Substring(0, line.IndexOf("="))" that makes it go wrong, but I have no idea whatsoever what can be done about it ... Any help? Thanks Opa Vito Opa Vito schreef:
> Hi, I'm stuck :-) I found it ... after trying for at least 5 hours ... :-)just had to insert this: If line IsNot Nothing Then 'SOLUTION element = line.Substring(0, line.IndexOf("=")) Form1.TextBox1.Text = Form1.TextBox1.Text & element & vbCrLf End If So checking if the line wasn't empty before looking for the substring .. Vito Hello Opa,
String manipulation (at least the limited and incorrect varienty offered in the previous postings) is not the answer. Win32 already provides this functionality (which you can easily wrap up in a class lib) in the form of GetPrivateProfileString (in kernel32.dll) and it's cohorts. -Boo Show quoteHide quote > Hello, I need to read data from a file the old fashioned way, more or > less like in the old days when ini files were used. But to be honest, > I can't find anything in the documentations (I'm not English, maybe I > just don't understand it). > > Let's assume I have this in the file (called setup.txt): > > [settings] > name1=Vito > name2=Doortje > txt1=married with children > txt2=and still happy > pic=both.jpg > My program must read this content and assign the data in the rigth > hand portion to a variable (declared public)with the name of the left > hand portion of each line, and above all the program must search ONLY > in the [settings] section ( there will be other additional sections in > the future) > > All tips very welcome, please understand I'm a newbie at programming > in VB and I'm not English ... > > Many thanks > > Vito > GhostInAK schreef:
> Hello Opa, Oops ... being a newbie, this sounds Chinese to me :-)> > String manipulation (at least the limited and incorrect varienty offered > in the previous postings) is not the answer. Win32 already provides > this functionality (which you can easily wrap up in a class lib) in the > form of GetPrivateProfileString (in kernel32.dll) and it's cohorts. > > -Boo Can you please give me some more info or maybe a bit code to start with? No need for fully functional code (however, if possible, that would be great too), but just something to get started ... Thanks Opa Vito Opa Vito wrote:
Show quoteHide quote > GhostInAK schreef: Go to www.pinvoke.net and look up the GetPrivateProfileString and> > Hello Opa, > > > > String manipulation (at least the limited and incorrect varienty offered > > in the previous postings) is not the answer. Win32 already provides > > this functionality (which you can easily wrap up in a class lib) in the > > form of GetPrivateProfileString (in kernel32.dll) and it's cohorts. > > > > -Boo > > Oops ... being a newbie, this sounds Chinese to me :-) > Can you please give me some more info or maybe a bit code to start with? > No need for fully functional code (however, if possible, that would be > great too), but just something to get started ... > SetPrivateProfileString functions for further information on these functions, but here is the VB example they listed (adjusted to match your file): Imports System.Runtime.InteropServices Imports System.Text Module1 Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _ ByVal lpKeyName As String, _ ByVal lpDefault As String, _ ByVal lpReturnedString As StringBuilder, _ ByVal nSize As Integer, _ ByVal lpFileName As String) As Integer Sub Main() Dim res As String Dim sb As StringBuilder sb = New StringBuilder(500) res = GetPrivateProfileString("settings", "name1", "", sb, sb.Capacity, "c:\test.ini") Console.WriteLine("GetPrivateProfileStirng returned : " & res) Console.WriteLine("KeyName is : " & sb.ToString()) End Sub End Module
Access the type from a shared method
Stored Procedures Web login problem VB.net 1.0 to VB.net Express Edition Procedures/Functions HTML Data generated by Server.Controls Error trying to connect to Access DB in VB.Net Embedding a device in an application? Analyzing Images? dAdapter.FillSchema vs dAdapter.Fill with dataset.clear? |
|||||||||||||||||||||||