Reading a selected file, finding a line that begins with a text, thn storing it 2 var

{IP}Gil-Galad

Member
Joined
Sep 25, 2005
Messages
19
Programming Experience
Beginner
well im working on a program that you can open a file, i searches through it for lines that begin in a word, stores that whole line as a variable, then the variables are stored in a combo box this is what i got so far.


VB.NET:
[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnArmor_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnArmor.Click

[color=#0000ff]With[/color] OpenFileDialog1

.Filter = "All Files (*.*)|*.*|" & _ 

"INI Files (*.ini)|*.ini"

.FilterIndex = 1

.Title = "Open Armor.ini"

.CheckFileExists() = [color=#0000ff]True

[/color]

.CheckPathExists = [color=#0000ff]True

[/color]

.ValidateNames = [color=#0000ff]True

[/color]

.DereferenceLinks = [color=#0000ff]True

[/color]

.InitialDirectory = "c:\"

.ShowDialog([color=#0000ff]Me[/color])

[color=#0000ff]If[/color] .FileName <> "" [color=#0000ff]Then[/color][color=#008000]

[/color][color=#0000ff]Dim[/color] sr [color=#0000ff]As[/color] IO.StreamReader = IO.File.OpenText(.FileName)

[color=#0000ff]

[/color]

[color=#0000ff]End[/color] [color=#0000ff]If

[/color]

[color=#0000ff]End[/color] [color=#0000ff]With

[/color]

[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]



it should read the file that i just selected. the problem is im a newbie so i need help what comes next.

Heres an example file.



hwufg
Armor cojrec
ihoivre
Armor rv

these lines (Armor cojrec, Armor rv) i want each stored to a variable so i can use that to add the lines above into a combo box.

I need this asap i no im asking alot but theres no other way around this.

any help would be deeply appreciated thank you
 
Hey,

The following code will read in the file, however there are lots of INI classes out there to make ini files easier to cope with, I'll post one later if I can remember where they are!

VB.NET:
[color=#0000ff]Try
[/color][color=#0000ff]Dim[/color] myFileStream [color=#0000ff]As[/color] [color=#0000ff]New[/color] System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
 
[color=#008000]' read from the file here
 
[/color][color=#0000ff]Dim[/color] myReader [color=#0000ff]As[/color] [color=#0000ff]New[/color] System.IO.StreamReader(myFileStream)
Dim hwufg, ihoivre,ArmorC,ArmorRV as String
[color=#008000]' read the entire text of the file
 
[/color]hwufg = myReader.ReadLine
 
ArmorC = myReader.ReadLine
 
ihoivre = myReader.ReadLine
 
ArmorRv = myReader.ReadLine
 
[color=#008000]' close the file when you are done
[/color]myReader.Close()
 
[color=#008000]' close the file when you are done
 
[/color]myFileStream.Close()
 
[color=#0000ff]Catch
 
[/color][color=#0000ff]MsgBox("Could not read file")[/color]
[color=#0000ff]
[/color][color=#0000ff]End[/color] [color=#0000ff]Try
 
[/color]

You can modify to check the headers by doing something like:

VB.NET:
dim header as string
 
Header = myReader.Readline
 
Select Case Header
Case "hwufg"
ArmorC = myReader.ReadLine
Case "ihoivre"
ArmorRV = myReader.ReadLine
End Select
 
Last edited:
um well what i meant is that it searches through the document. the placement of the things are uknown so doing that wont work. the lines tha begin with "Armor " need to be stored into a variable. I already knew that stuff above but what i need is a find feature where it searches through the text and looks for a word, then it stores the line as a variable. Sorry for the misunderstandment
 
Thanx Alot :D

Ok this is what i got except i dont understand it (noobishness standing out again)

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] sValue [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sPath [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]sPath = .FileName 
 
INIWrite(sPath, "Section1", "Key1-1", "Value1-1") [/size][size=2][color=#008000]' build INI file
 
[/color][/size][size=2]
 
INIWrite(sPath, "Section1", "Key1-2", "Value1-2")
 
INIWrite(sPath, "Section1", "Key1-3", "Value1-3")
 
INIWrite(sPath, "Section2", "Key2-1", "Value2-1")
 
INIWrite(sPath, "Section2", "Key2-2", "Value2-2")
 
sValue = INIRead(sPath, "section2", "key2-1", "Unknown") [/size][size=2][color=#008000]' specify all
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "section2/key2-1/unknown", MessageBoxButtons.OK)
 
sValue = INIRead(sPath, "section2", "XYZ", "Unknown") [/size][size=2][color=#008000]' specify all
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "section2/xyz/unknown", MessageBoxButtons.OK)
 
sValue = INIRead(sPath, "section2", "XYZ") [/size][size=2][color=#008000]' use zero-length string as default
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "section2/XYZ", MessageBoxButtons.OK)
 
sValue = INIRead(sPath, "section1") [/size][size=2][color=#008000]' get all keys in section
 
[/color][/size][size=2]
 
sValue = sValue.Replace(ControlChars.NullChar, "|"c) [/size][size=2][color=#008000]' change embedded NULLs to pipe chars
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "section1 pre delete", MessageBoxButtons.OK)
 
INIDelete(sPath, "section1", "key1-2") [/size][size=2][color=#008000]' delete middle entry in section 1
 
[/color][/size][size=2]
 
sValue = INIRead(sPath, "section1") [/size][size=2][color=#008000]' get all keys in section again
 
[/color][/size][size=2]
 
sValue = sValue.Replace(ControlChars.NullChar, "|"c) [/size][size=2][color=#008000]' change embedded NULLs to pipe chars
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "section1 post delete", MessageBoxButtons.OK)
 
sValue = INIRead(sPath) [/size][size=2][color=#008000]' get all section names
 
[/color][/size][size=2]
 
sValue = sValue.Replace(ControlChars.NullChar, "|"c) [/size][size=2][color=#008000]' change embedded NULLs to pipe chars
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "All sections pre delete", MessageBoxButtons.OK)
 
INIDelete(sPath, "section1") [/size][size=2][color=#008000]' delete section
 
[/color][/size][size=2]
 
sValue = INIRead(sPath) [/size][size=2][color=#008000]' get all section names
 
[/color][/size][size=2]
 
sValue = sValue.Replace(ControlChars.NullChar, "|"c) [/size][size=2][color=#008000]' change embedded NULLs to pipe chars
 
[/color][/size][size=2]
 
MessageBox.Show(sValue, "All sections post delete", MessageBoxButtons.OK)
 
[/size]

obviously i need to read the sections that begin with Armor. then put that line in a variable or directly store it into a combobox.

heres an example of what the sections look like.


VB.NET:
Armor HeroLightArmor
	  Armor = DEFAULT 100% ;this sets the level for all nonspecified damage types
	  Armor = PIERCE 50% ;archer damage
	  Armor = CRUSH 60% ;40% ;cavalry damage
	  Armor = SLASH 60% ;soldier damage
	  Armor = URUK 60% ;Uruk damage
	  Armor = HERO 100% ;Hero damage
	  Armor = HERO_RANGED 100% ;Hero bow damage
	  Armor = FLAME 40% ;fire damage
End
Armor HeroArmor
	  Armor = DEFAULT 75% ;this sets the level for all nonspecified damage types
	  Armor = PIERCE 50% ;archer damage
	  Armor = CRUSH 40% ;20% ;cavalry damage
	  Armor = SLASH 75% ;soldier damage
	  Armor = URUK 50% ;Uruk damage
	  Armor = HERO 100% ;Hero damage
	  Armor = HERO_RANGED 100% ;Hero bow damage
	  Armor = FLAME 30% ;fire damage
	  Armor = SIEGE 100%
End
Armor is the section name and i just need the section not whats in it.
If any one can give pointers that would be amazing :D

 
Back
Top