Getting text from within a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file

[88games
gamename='88 Game
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails=This is a 4 player team type game where 2 players compete each tim
P1NumButtons=
P1Controls=Just Buttons+butto
P1_BUTTON1=Ru
P1_BUTTON2=Jum
P1_BUTTON3=Ru

[005
gamename=00
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails
P1NumButtons=
P1Controls=4-way Joystick+joy4wa
P1_BUTTON1=Fir
P1_JOYSTICK_UP=U
P1_JOYSTICK_DOWN=Dow
P1_JOYSTICK_LEFT=Lef
P1_JOYSTICK_RIGHT=Righ

[yard

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the “[005]†section. I have the code returning what character the string “[005]†starts at but don’t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable

After that I want to do the same thing, kind of, and put the parts after the “=†sign into separate labels that I have on my form

What is my best way to accomplish this

Thank you
Joh
 
I?m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file:

[88games]
gamename='88 Games
numPlayers=2
alternating=0
mirrored=1
tilt=0
usesService=0
miscDetails=This is a 4 player team type game where 2 players compete each time
P1NumButtons=3
P1Controls=Just Buttons+button
P1_BUTTON1=Run
P1_BUTTON2=Jump
P1_BUTTON3=Run

[005]
gamename=005
numPlayers=2
alternating=1
mirrored=1
tilt=0
usesService=0
miscDetails=
P1NumButtons=1
P1Controls=4-way Joystick+joy4way
P1_BUTTON1=Fire
P1_JOYSTICK_UP=Up
P1_JOYSTICK_DOWN=Down
P1_JOYSTICK_LEFT=Left
P1_JOYSTICK_RIGHT=Right

[yard]

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the ?[005]? section. I have the code returning what character the string ?[005]? starts at but don?t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable.

After that I want to do the same thing, kind of, and put the parts after the ?=? sign into separate labels that I have on my form.

What is my best way to accomplish this?

Thank you,
John

Hmm, looks like a standard windows ini file... Personally, I would
suggest using P/Invoke to call GetPrivateProfileString to get the pieces
you want. Others may disagree, but why write a parsing routine for
something the system already handles:)

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

You can read an entire section into a single buffer by passing Nothing
to the lpKeyName parameter. It will fill the buffer with all keys in
the section separated by null chars...

You're other option is to look into System.Text.RegularExpressions to
extract the piece of data you would like from the string...
 
jcrouse said:
I'm using VB .Net 2003. I am reading a file into memory. Here is a
sniplet of the file:
<omitted>

It is quite large. Anyways. Now that I have it into a variable I
would like to pull out a specific section into another variable. Say
the "[005]" section. I have the code returning what character the
string "[005]" starts at but don't know the command or syntax to
start at a certain character number (which I have) and then read
until the first blank line then put it into another variable.

You probably don't want to be doing this in memory, especially since you say
the file is quite large.
Why not instead read the file line by line using StreamReader.ReadLine, and
parse the file as you go. Just keep reading lines and discarding them until
you get the line that reads "[005]", then read the lines after that parsing
them until you get to a blank line.
After that I want to do the same thing, kind of, and put the parts
after the "=" sign into separate labels that I have on my form.

You'd want to use String.Split for that.
 
That's all well and dandy but I need code. As earlier stated, I can't seem to store or retrieve anything but the starting character numer of the beginning of the matching expression. How do I do a StreamReader.ReadLine and then put it into a varaible or label.text property

Thank you
John
 
What do I do with your sample code. Pardon me for asking, I'm a little confused and new at this. How do I invoke it in startup? I obviously need to fill the variables somehow

HELP
John
 
Tom....Here is my code. It keeps underlining the word declare and saying that "Keyword is not valid as an identifier"? I'm stumped

Joh


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Clic

Public Declare Function GetPrivateProfileString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As System.Text.StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Intege

lpAppName = lblInput.Tex
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedString = (256
lpNsize = Len(lpReturnedString
lpFileName = "C:\CPViewer\Controls.ini


End Su
 
John,
Put the declare outside the Sub! As the Declare is used to declare a
Function or a Sub that is external to the program, remember that VB does not
allow subs & functions to be nested inside of other subs or functions.
Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
Hope this helps
Jay

jcrouse said:
Tom....Here is my code. It keeps underlining the word declare and saying
that "Keyword is not valid as an identifier"? I'm stumped.
John



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
 
Well...here's my code

Private Declare Auto Function GetPrivateProfileString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As System.Text.StringBuilder,
ByVal lpNsize As Integer,
ByVal lpFileName As String) As Intege

Dim x As Lon

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Clic

lpAppName = "1942
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedString = (256
lpNsize = Len(lpReturnedString
lpFileName = "C:\CPViewer\Controls.ini

x = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, lpNsize, lpFileName

lblOutput.Text =

End Su

Now I get an error on button click that says

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.ex
Additional information: Specified cast is not valid

Anymore ideas? I'm close here. I think the parameters for the buffer may be wrong
John
 
see inline...


jcrouse said:
Well...here's my code:

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal lpNsize As Integer, _
ByVal lpFileName As String) As Integer

Dim x As Long

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
lpAppName = "1942"
lpKeyName = "P1_BUTTON1"
lpDefault = ""
lpReturnedString = (256) <<==== what is this?????

try instead
lpReturnedString = New System.Text.StringBuilder(256)

then this
lpNsize = Len(lpReturnedString)

becomes
lpNsize = 256

or if you like

lpNsize = lpReturnedString.Length
lpFileName = "C:\CPViewer\Controls.ini"


x = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault,
lpReturnedString, lpNsize, lpFileName)
 
Tom....Here is my code. It keeps underlining the word declare and saying that "Keyword is not valid as an identifier"? I'm stumped.

John



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

lpAppName = lblInput.Text
lpKeyName = "P1_BUTTON1"
lpDefault = ""
lpReturnedString = (256)
lpNsize = Len(lpReturnedString)
lpFileName = "C:\CPViewer\Controls.ini"


End Sub

Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn1.Click

Dim lpReturnedString As New System.Text.StringBuilder(256)

GetPrivateProfileString( _
"88games",
"P1_BUTTON1",
String.Empty,
lpReturnedString,
lpReturnedString.Capacity,
"C:\CPViewer\Controls.ini"

MessageBox.Show("P1_BUTTON1=" & lpReturnedString.ToString())
End Sub

HTH
 
Back
Top