Results 1 to 6 of 6

Thread: [RESOLVED] Help please! Stuck on .ini files

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Resolved [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

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: Help please! Stuck on .ini files

    Quote Originally Posted by IanRyder View Post
    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

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help please! Stuck on .ini files

    I'd do this in a DataGridView with 1 textbox column.

    vb.net Code:
    1. Public Class Form1
    2.     ' DataGridView settings
    3.     ' AllowUserToAddRows = False
    4.     ' RowHeadersWidthSizeMode = AutoSizeToAllHeaders
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.         For Each line In IO.File.ReadLines("C:\test.ini")
    8.             If line.Contains("=") Then
    9.                 Dim ll = line.Split({" = "}, StringSplitOptions.RemoveEmptyEntries)
    10.                 Dim dgvRow = DataGridView1.Rows.Add(1)
    11.                 DataGridView1.Rows(dgvRow).HeaderCell.Value = ll(0)
    12.                 DataGridView1.Rows(dgvRow).Cells(0).Value = ll(1)
    13.             End If
    14.         Next
    15.     End Sub
    16.  
    17.     Private Sub Save_Click(sender As System.Object, e As System.EventArgs) Handles Save.Click
    18.         Dim s As New System.Text.StringBuilder
    19.         s.Append(vbCrLf & "[client]" & vbCrLf)
    20.         For Each rw As DataGridViewRow In DataGridView1.Rows
    21.             s.AppendLine(rw.HeaderCell.Value.ToString & " = " & rw.Cells(0).Value.ToString)
    22.         Next
    23.         IO.File.WriteAllText("C:\test.ini", s.ToString)
    24.     End Sub
    25. 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!

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: Help please! Stuck on .ini files

    Quote Originally Posted by dunfiddlin View Post
    I'd do this in a DataGridView with 1 textbox column.

    vb.net Code:
    1. Public Class Form1
    2.     ' DataGridView settings
    3.     ' AllowUserToAddRows = False
    4.     ' RowHeadersWidthSizeMode = AutoSizeToAllHeaders
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.         For Each line In IO.File.ReadLines("C:\test.ini")
    8.             If line.Contains("=") Then
    9.                 Dim ll = line.Split({" = "}, StringSplitOptions.RemoveEmptyEntries)
    10.                 Dim dgvRow = DataGridView1.Rows.Add(1)
    11.                 DataGridView1.Rows(dgvRow).HeaderCell.Value = ll(0)
    12.                 DataGridView1.Rows(dgvRow).Cells(0).Value = ll(1)
    13.             End If
    14.         Next
    15.     End Sub
    16.  
    17.     Private Sub Save_Click(sender As System.Object, e As System.EventArgs) Handles Save.Click
    18.         Dim s As New System.Text.StringBuilder
    19.         s.Append(vbCrLf & "[client]" & vbCrLf)
    20.         For Each rw As DataGridViewRow In DataGridView1.Rows
    21.             s.AppendLine(rw.HeaderCell.Value.ToString & " = " & rw.Cells(0).Value.ToString)
    22.         Next
    23.         IO.File.WriteAllText("C:\test.ini", s.ToString)
    24.     End Sub
    25. 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
  •  



Click Here to Expand Forum to Full Width