|
-
May 3rd, 2013, 06:46 PM
#1
Thread Starter
New Member
[RESOLVED] Help please! Stuck on .ini files
Hello there. Basically i have been coding a program and i've got to a part where now i need to have a windows form that has 11 text boxes, each text box reading a corresponding line from a config.ini file on my c drive. Then i want to make it so i have a write button which will write to the file any changes i have made in the textboxes. The config.ini has the following contents :
{ignore this line but in the file i need this to be a empty line so that the ini file starts 2 lines down}
[client]
name = ray
xres = 800
yres = 600
vol = 10
inverty = 0
windowed = 1
language = 0
mouse_sensitivity = 5.000000
show_news = 1
now comes the real challenge. i want to make it so i can only see everything after the = in the config file inside the textbox so that i am only changing the value at the end of the line, is this possible to do? and if so could some absolute life saver do this on a forms1 so i could set it up & test it. it would be greatly appreciated. thanks
-
May 3rd, 2013, 09:56 PM
#2
Re: Help please! Stuck on .ini files
Hi,
Here is a start for you:-
1) Use a StreamReader to read the file line by line. Have a look here:-
http://msdn.microsoft.com/en-us/libr...code-snippet-1
2) After reading each line use the Split Method of the String Class to split the line by the space character to create an Array of words. Have a look here:-
http://msdn.microsoft.com/en-us/library/b873y76a.aspx
3) Then check for the Length of the Array being 3. If the length of the Array is 3 then you know that it defines a configuration statement where element 0 defines the field to be populated and element 2 defines the data to be assigned to the field. You can then use an If or Case statement to populate the right TextBox's on your form.
4) Once you have completed your updates you can use a StreamWriter to save your formatted strings back to the file. Have a look here:-
http://msdn.microsoft.com/en-us/libr...eamwriter.aspx
Hope that helps.
Cheers,
Ian
-
May 4th, 2013, 04:10 AM
#3
Thread Starter
New Member
Re: Help please! Stuck on .ini files
Thank you Ian this is greatly appreciated, I will attempt to do this and get back to you on how I do.
Once again. Thank you.
-
May 4th, 2013, 05:04 PM
#4
Thread Starter
New Member
Re: Help please! Stuck on .ini files
 Originally Posted by IanRyder
Hi,
Here is a start for you:-
1) Use a StreamReader to read the file line by line. Have a look here:-
http://msdn.microsoft.com/en-us/libr...code-snippet-1
2) After reading each line use the Split Method of the String Class to split the line by the space character to create an Array of words. Have a look here:-
http://msdn.microsoft.com/en-us/library/b873y76a.aspx
3) Then check for the Length of the Array being 3. If the length of the Array is 3 then you know that it defines a configuration statement where element 0 defines the field to be populated and element 2 defines the data to be assigned to the field. You can then use an If or Case statement to populate the right TextBox's on your form.
4) Once you have completed your updates you can use a StreamWriter to save your formatted strings back to the file. Have a look here:-
http://msdn.microsoft.com/en-us/libr...eamwriter.aspx
Hope that helps.
Cheers,
Ian
Thanks again for this Ian but after several hours of trying and multiple breakdowns, the answer eludes me. is this impossible? i think this might be beyond my scope. do you happen to have an example project of this. if so it will greatly be appreciated. Thanks, below i have attached the config file.
Example.zip
-
May 4th, 2013, 06:56 PM
#5
Re: Help please! Stuck on .ini files
I'd do this in a DataGridView with 1 textbox column.
vb.net Code:
Public Class Form1
' DataGridView settings
' AllowUserToAddRows = False
' RowHeadersWidthSizeMode = AutoSizeToAllHeaders
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For Each line In IO.File.ReadLines("C:\test.ini")
If line.Contains("=") Then
Dim ll = line.Split({" = "}, StringSplitOptions.RemoveEmptyEntries)
Dim dgvRow = DataGridView1.Rows.Add(1)
DataGridView1.Rows(dgvRow).HeaderCell.Value = ll(0)
DataGridView1.Rows(dgvRow).Cells(0).Value = ll(1)
End If
Next
End Sub
Private Sub Save_Click(sender As System.Object, e As System.EventArgs) Handles Save.Click
Dim s As New System.Text.StringBuilder
s.Append(vbCrLf & "[client]" & vbCrLf)
For Each rw As DataGridViewRow In DataGridView1.Rows
s.AppendLine(rw.HeaderCell.Value.ToString & " = " & rw.Cells(0).Value.ToString)
Next
IO.File.WriteAllText("C:\test.ini", s.ToString)
End Sub
End Class
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 4th, 2013, 07:42 PM
#6
Thread Starter
New Member
Re: Help please! Stuck on .ini files
 Originally Posted by dunfiddlin
I'd do this in a DataGridView with 1 textbox column.
vb.net Code:
Public Class Form1 ' DataGridView settings ' AllowUserToAddRows = False ' RowHeadersWidthSizeMode = AutoSizeToAllHeaders Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load For Each line In IO.File.ReadLines("C:\test.ini") If line.Contains("=") Then Dim ll = line.Split({" = "}, StringSplitOptions.RemoveEmptyEntries) Dim dgvRow = DataGridView1.Rows.Add(1) DataGridView1.Rows(dgvRow).HeaderCell.Value = ll(0) DataGridView1.Rows(dgvRow).Cells(0).Value = ll(1) End If Next End Sub Private Sub Save_Click(sender As System.Object, e As System.EventArgs) Handles Save.Click Dim s As New System.Text.StringBuilder s.Append(vbCrLf & "[client]" & vbCrLf) For Each rw As DataGridViewRow In DataGridView1.Rows s.AppendLine(rw.HeaderCell.Value.ToString & " = " & rw.Cells(0).Value.ToString) Next IO.File.WriteAllText("C:\test.ini", s.ToString) End Sub End Class
Thank you i have managed to implement this quite well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|