Code:
To write:
Public Sub ReplaceText(ByVal FileName As String, ByVal StartStr As String, ByVal ReplaceWith As String)
Dim oldValue As String = ""
Dim str As String = ""
If File.Exists(FileName) Then
Using reader As StreamReader = New StreamReader(FileName)
Do While Not reader.EndOfStream
oldValue = reader.ReadLine
If oldValue.Trim.ToLower.Contains(StartStr.Trim.ToLower) Then
oldValue = oldValue.Replace(oldValue, ReplaceWith)
End If
str = (str & oldValue & ChrW(13) & ChrW(10))
Loop
End Using
Using writer As StreamWriter = New StreamWriter(FileName, False)
writer.Write(str)
End Using
End If
End Sub
To read:
Public Function ReadSpecifiedLine(ByVal file As String, ByVal lineNum As Integer) As String
Dim contents As String = String.Empty
Try
Using stream As New StreamReader(file)
contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})
If linesArray.Length > 1 Then
If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
Return (linesArray(lineNum))
Else
Return (linesArray(0))
End If
Else
Return (contents)
End If
End Using
Catch ex As Exception
Return (ex.ToString())
End Try
End Function
Funcs are not mine, read the credits.
Code:
Public Lang As String = ReadSpecificLine(CurDir() & "\settings.ini", 0).Replace("Language = ", "") 'This variable will hold our current language name
Public Sub GetLanguage()
'easy if stat.
If Lang = "English" Then _
Label1.Text = "My name is Elio"
If Lang = "Italian" Then Label1.Text = "Il mio nome e Elio" 'I can't write very good on italian and english too btw :p
End Sub
Public Sub SetLanguage()
ReplaceText(CurDir() & "\settings.ini", "Language =", "Language = " & TextBox1.Text) 'Replaces the current language with an other
'Recommended: Make a List/Array of strings which contains the supported languages to set, to make sure that while the run-time the app. will never crash.
End Sub
The functions are now done.