Help with replace with a string
Here's the code:
vb Code:
Private Sub btnSaveSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSettings.Click
Dim reader As String
reader = My.Computer.FileSystem.ReadAllText(config_file)
Replace(reader, Music_path, txtMusic_Path.Text)
Replace(reader, Movies_path, txtMovie_Path.Text)
Replace(reader, Shows_path, txtShows_Path.Text)
MessageBox.Show(reader)
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!
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
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.