473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting text from within a string

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=Thi s 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_DOW N=Dow
P1_JOYSTICK_LEF T=Lef
P1_JOYSTICK_RIG HT=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

Nov 20 '05 #1
11 1785
In article <06************ *************** *******@microso ft.com>, jcrouse wrote:
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=Thi s 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+joy4wa y
P1_BUTTON1=Fire
P1_JOYSTICK_UP= Up
P1_JOYSTICK_DOW N=Down
P1_JOYSTICK_LEF T=Left
P1_JOYSTICK_RIG HT=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 GetPrivateProfi leString 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 GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
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.Reg ularExpressions to
extract the piece of data you would like from the string...

--
Tom Shelton [MVP]
Nov 20 '05 #2
jcrouse wrote:
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.Re adLine, 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.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #3
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.Re adLine and then put it into a varaible or label.text property

Thank you
John
Nov 20 '05 #4
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
Nov 20 '05 #5
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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Clic

Public Declare Function GetPrivateProfi leString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedStrin g As System.Text.Str ingBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Intege

lpAppName = lblInput.Tex
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedStrin g = (256
lpNsize = Len(lpReturnedS tring
lpFileName = "C:\CPViewer\Co ntrols.ini
End Su

Nov 20 '05 #6
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 GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Click
Hope this helps
Jay

"jcrouse" <an*******@disc ussions.microso ft.com> wrote in message
news:BF******** *************** ***********@mic rosoft.com... 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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Click
Public Declare Function GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

lpAppName = lblInput.Text
lpKeyName = "P1_BUTTON1 "
lpDefault = ""
lpReturnedStrin g = (256)
lpNsize = Len(lpReturnedS tring)
lpFileName = "C:\CPViewer\Co ntrols.ini"
End Sub

Nov 20 '05 #7
Well...here's my code

Private Declare Auto Function GetPrivateProfi leString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedStrin g As System.Text.Str ingBuilder,
ByVal lpNsize As Integer,
ByVal lpFileName As String) As Intege

Dim x As Lon

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Clic

lpAppName = "1942
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedStrin g = (256
lpNsize = Len(lpReturnedS tring
lpFileName = "C:\CPViewer\Co ntrols.ini

x = GetPrivateProfi leString(lpAppN ame, lpKeyName, lpDefault, lpReturnedStrin g, lpNsize, lpFileName

lblOutput.Text =

End Su

Now I get an error on button click that says

An unhandled exception of type 'System.Invalid CastException' occurred in WindowsApplicat ion2.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
Nov 20 '05 #8
see inline...
"jcrouse" <an*******@disc ussions.microso ft.com> wrote in message
news:84******** *************** ***********@mic rosoft.com...
Well...here's my code:

Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
ByVal lpNsize As Integer, _
ByVal lpFileName As String) As Integer

Dim x As Long

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Click
lpAppName = "1942"
lpKeyName = "P1_BUTTON1 "
lpDefault = ""
lpReturnedStrin g = (256) <<==== what is this?????
try instead
lpReturnedStrin g = New System.Text.Str ingBuilder(256)

then this lpNsize = Len(lpReturnedS tring)
becomes
lpNsize = 256

or if you like

lpNsize = lpReturnedStrin g.Length
lpFileName = "C:\CPViewer\Co ntrols.ini"

x = GetPrivateProfi leString(lpAppN ame, lpKeyName, lpDefault, lpReturnedStrin g, lpNsize, lpFileName)
lblOutput.Text = x

End Sub

Now I get an error on button click that says:

An unhandled exception of type 'System.Invalid CastException' occurred in WindowsApplicat ion2.exe Additional information: Specified cast is not valid.

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

Nov 20 '05 #9
On Wed, 26 May 2004 19:16:03 -0700, jcrouse wrote:
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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btn1.Click

Public Declare Function GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

lpAppName = lblInput.Text
lpKeyName = "P1_BUTTON1 "
lpDefault = ""
lpReturnedStrin g = (256)
lpNsize = Len(lpReturnedS tring)
lpFileName = "C:\CPViewer\Co ntrols.ini"
End Sub


Public Declare Function GetPrivateProfi leString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles btn1.Click

Dim lpReturnedStrin g As New System.Text.Str ingBuilder(256)

GetPrivateProfi leString( _
"88games",
"P1_BUTTON1 ",
String.Empty,
lpReturnedStrin g,
lpReturnedStrin g.Capacity,
"C:\CPViewer\Co ntrols.ini"

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

HTH
--
Tom Shelton [MVP]
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1176
by: DC Gringo | last post by:
I am a bit twisted and need some straightening out. I have a webform with 3 controls on it: a mainHeader, a sectionHeader with a label control contained in the mainHeader, and a topHeadline. In the topHeadline, I am retrieving a value with a stored procedure. I need to set that value to a label control within the sectionHeader like this: mainHeader1.sectionHeader1.sectionHeaderLabel.Text = myVariable In topHeadlines, I have:
10
2239
by: David B | last post by:
I am writing a console App with VB 2005 where I accept a text file as input, parse it and insert the text data into an SQL table. I have written the code correctly as well as I can tell because it works running it within the IDE, given that you have to go into the Debug Project Properties to specify the argument. If I build the file and go run it from the command line, specifying the argument, I get the following error: ...
1
1879
by: Jerry John | last post by:
I am working in ASP.NET with C#. I have a text file which contains datas with delimiters. For example:- MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939| I also have an XML file created with predefined tags. Some of the tags contain child element. I need to pass the data from the text file i.e the value within the delimiters should be passed to the corresponding tags within the XML file. I have done this through hard code. But i...
3
2083
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: ************************************************* Function RandomString(size as integer, lowerCase as boolean) as string Dim builder as StringBuilder = new StringBuilder() Dim random as Random = new Random() Dim i as integer dim ch as char
3
22530
by: GaryDean | last post by:
I have a button field in a gridview defined as follows: <asp:ButtonField ButtonType="Button" CommandName="AcceptOrder" Text="Accept Whole Order"> <ControlStyle Font-Size="X-Small" /> </asp:ButtonField> In the RowCommand event I have the following code: if ((string)e.CommandName.ToLower() == "acceptorder") { int myRow = int.Parse(e.CommandArgument.ToString());
12
3578
by: Angus | last post by:
I am writing a class as a wrapper around a std::map. The class is: template<class TKey, class TValue> class CGeneralMap : protected map<TKey, TValue> At the moment I have basic functions like Add, Remove etc but I am really struggling with GetValue, which will be like the value obtained from
2
6658
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI Week5MortgageLogic allint = logic.allInterest(amount, term, rate); Week5MortgageGUI.java:152:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI
3
1197
by: MDCdeveloper | last post by:
Hello all, and thank you in advance for any feedback/help you might post. I need help getting a web page element id out of a string variable to be used within my c# code behind page. I am trying to display a string in a literal control telling the user what was wrong with the input that they provided in the associated text box. my error string looks like this: errorMsg = "litLastName-Please enter a valid last name" I already have...
2
2053
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue only half of the diagram is getting painted,some lines are missing in the diagram. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { string str=listBox1.SelectedItem.ToString(); string str1=null;...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.