Try a regex:
public static Regex regex = new Regex("(?<=SERVER\\s*=\\s*)localhost(?=\\$)",
RegexOptions.Multiline
| RegexOptions.Singleline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
...
string inputText = File.ReadAllText(pathToFile);
string result = regex.Replace(inputText,"Hello world!");