Results 1 to 3 of 3

Thread: Help with replace with a string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Help with replace with a string

    Here's the code:

    vb Code:
    1. Private Sub btnSaveSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSettings.Click
    2.   Dim reader As String
    3.   reader = My.Computer.FileSystem.ReadAllText(config_file)
    4.   Replace(reader, Music_path, txtMusic_Path.Text)
    5.   Replace(reader, Movies_path, txtMovie_Path.Text)
    6.   Replace(reader, Shows_path, txtShows_Path.Text)
    7.   MessageBox.Show(reader)
    8. End Sub

    Upon running this section of code reader is being set to.
    [MUSIC_PATH]=X:\Multimedia\Audio
    [MOVIES_PATH]=C:\Media\Movies
    [SHOWS_PATH]=C:\Media\TV Shows

    When the code is run the textboxes values are as follow.
    txtMusic_Path = X:\Multimedia\Audio
    txtMovies_Path = C:\Media
    txtShows_Path = C:\Media\TV Shows

    At the end when the MessageBox.Show() is run it displays.
    [MUSIC_PATH]=X:\Multimedia\Audio
    [MOVIES_PATH]=C:\Media\Movies
    [SHOWS_PATH]=C:\Media\TV Shows

    Why isn't it coming out as this?
    [MUSIC_PATH]=X:\Multimedia\Audio
    [MOVIES_PATH]=C:\Media
    [SHOWS_PATH]=C:\Media\TV Shows

    Thanks!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Help with replace with a string

    Try this:
    Code:
    Private Sub btnSaveSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSettings.Click
            Dim fileContents() As String = System.IO.File.ReadAllLines(config_file)
            Dim line As String = String.Empty
            For i As Integer = 0 To fileContents.Length - 1
                line = line.Trim()
                If line.StartsWith("[MUSIC_PATH]") Then
                    line = "[MUSIC_PATH]=" & txtMusic_Path.Text
                ElseIf line.StartsWith("[MOVIES_PATH]") Then
                    line = "[MOVIES_PATH]=" & txtMovie_Path.Text
                ElseIf line.StartsWith("[SHOWS_PATH]") Then
                    line = "[SHOWS_PATH]=" & txtShows_Path.Text
                End If
                fileContents(i) = line
            Next
            System.IO.File.WriteAllLines(config_file, fileContents)
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Help with replace with a string

    Replace is a function not a sub, you aren't doing anything with the return values. Also I would recommend using the Replace function on the string class instead of the legacy one.

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