I have two function that replace certain strings in a text file. The first one works perfectly:
Dim readAlias As String
Dim RxDIR As String = Nothing
Try
RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
RxDIR = RxDIR + "\Bin\Configuration\alias.config"
Dim sr As StreamReader = File.OpenText(RxDIR)
readAlias = (sr.ReadToEnd())
sr.Close()
sr.Dispose()
Dim aliasResults As String = readAlias.ToString
Dim aliasMainDB As String = "database=""(?<name>.+)"""
Dim m As Match = Regex.Match(readAlias, aliasMainDB, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
If (m.Success) Then
'MessageBox.Show(m.Groups("name").Value)
Dim replace As String = m.Groups("name").Value
readAlias = Regex.Replace(readAlias, replace, cbListDB.SelectedItem, RegexOptions.IgnoreCase)
Dim sw As StreamWriter = New StreamWriter(RxDIR)
sw.Write(readAlias)
sw.Close()
sw.Dispose()
Call mainConfig()
End If
Catch ex As Exception
End Try
The second one runs without error but does not make any changes:
Dim readAlias As String
Dim RxDIR As String = Nothing
Try
workingSQLconnection = "Data Source= " + NameSQL + " ;Initial Catalog=Master;User ID=**;Password=**"
RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
RxDIR = RxDIR + "\Bin\Configuration\Templates\alias.config"
Dim sr As StreamReader = File.OpenText(RxDIR)
readAlias = (sr.ReadToEnd())
sr.Close()
sr.Dispose()
Dim aliasResults As String = readAlias.ToString
Dim aliasServer As String = "server=""(?<name>.+)"""
Dim m As Match = Regex.Match(readAlias, aliasServer, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
If (m.Success) Then
Dim replace As String = m.Groups("name").Value
readAlias = Regex.Replace(aliasResults, replace, NameSQL, RegexOptions.IgnoreCase)
Dim sd As StreamWriter = New StreamWriter(RxDIR)
sd.Write(readAlias)
sd.Close()
sd.Dispose()
End If
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
The first one is setting a SQL database name ie: mainDB or backupDB.
The second sub is setting the SQL server instance ie: MYPC\SQLEXPRESS
Both are within private functions and the text file is the same alias.config text file.