Jump to content

Read particular line in txt document

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Danerd100

Danerd100

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Okay, I was wondering if there is a way to read and set a variable based on a certain line in a text file

IE

I have a configuration file that has a product key. The product key is the fifth line in the configuration file. How do I read just the product key?

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Are you using .NET? If so, I would suggest storing the configuration file as an XML File. I could show you how to extract the data easily - but only with .NET.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
Danerd100

Danerd100

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
I am using kbasic start [KBasic]

It uses the same syntax as VB, and i believe it is xml compatible. Could u show me how in .NET?

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Use the System.Xml namespace. Say you have a structure such as this in your file:

[highlight=xml]
<?xml version="1.0">
<config>
<productkey>149JJF9349</productkey>
<othersetting>value</othersetting>
</config>
[/highlight]

Like that. Then, in code:

[highlight=vb]
Dim doc As New System.Xml.XmlDocument()
doc.Load(IO.File.ReadAllText("C:\temp\config.xml"))

Dim ProductKey As String = doc.SelectSingleNode("config/productkey").InnerText.ToString()
[/highlight]
See?

Edited by Xav, 29 May 2008 - 11:48 AM.

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
In Visual Basic 6.0 you can use the InStr function.

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You can use that in .NET as well, I believe. An XML document is still the best way. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums