redacted
Printable View
redacted
try this:
vb Code:
Public Class Form1 Dim firstParts() As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})}) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lines() As String = IO.File.ReadAllLines("filename") ReDim firstParts(lines.GetUpperBound(0)) Dim parts() As String = lines(0).Split("="c) firstParts(0) = parts(0) TextBox1.Text = parts(1) parts = lines(1).Split("="c) firstParts(1) = parts(0) TextBox2.Text = parts(1) End Sub End Class
If you have any control over the format of this file, I would suggest that you consider something like XML. After all, XML structures a text file with nodes and data associated with those nodes. Your description suggests that you don't want to be making random changes to the file, but to actually only change the values associated with certain tags. That's much more aligned with XML than pure text files.
redacted
vb Code:
Public Class Form1 'array to store first parts of lines Dim firstParts() As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'save changes. WriteAllLines saves a string array, so i created a string array, + rejoined the lines IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})}) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'read the file into an array of strings, each element being 1 line of the file Dim lines() As String = IO.File.ReadAllLines("filename") 'dimension the firstParts array ReDim firstParts(lines.GetUpperBound(0)) 'split the first line into 2 parts Dim parts() As String = lines(0).Split("="c) 'save the first part to the array firstParts(0) = parts(0) 'put the second part in TextBox1 TextBox1.Text = parts(1) 'split the second line into 2 parts parts = lines(1).Split("="c) 'save the first part to the array firstParts(1) = parts(0) 'put the second part in TextBox2 TextBox2.Text = parts(1) End Sub End Class
here's an xml example:
vb Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xml As XDocument = _ <?xml version="1.0" encoding="utf-8"?> <root> </root> xml...<root>(0).Add( _ <people> <name><%= TextBox1.Text %></name> <age><%= TextBox2.Text %></age> </people>) xml.Save("test.xml") End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim xml As XDocument = Nothing If IO.File.Exists("test.xml") Then xml = XDocument.Load("test.xml") Dim info = (From node In xml...<people> _ Select New With { _ .name = node.<name>.Value, _ .age = node.<age>.Value}).ToArray TextBox1.Text = info(0).name TextBox2.Text = info(0).age End If End Sub End Class
redacted
it creates an xml file like this when you click the button.
vb Code:
<?xml version="1.0" encoding="utf-8"?> <root> <people> <name>Jim</name> <age>51</age> </people> </root>
in the form_load event, it checks if the xml file exists, + reads it.
LINQ tutorial:
http://searchwindevelopment.techtarg...de-LINQ-to-XML
One thing I would add is that XML is particularly easy to work with if you are dealing with a datatable, since you can serialize the table to XML with a single line, and deserialize back from XML just as easily. Considering your sample data, I would expect that you probably have more than one person (or else there are even better alternatives than XML), so you are probably storing the information somewhere. If you are using a datatable, then XML is definitely going to be better.