This is an odd one but I seem to be unable to solve it.
I'm creating a visual user interface to modify internet settings on Debian Wheezy (and other debian versions/derivatives that are incidentally compatible).
I want to be able to modify the DNS on the basis of what the user inserts.
After "Save" is pressed this code is ran:
void SaveDNSButton_event(object obj, ButtonPressEventArgs args)
{
//save dns settings
string s1 = "";
string s2 = "";
string toWrite = s1 + s2;
Console.WriteLine ("=============");
Console.WriteLine ("Reading from resolv.conf before writing...");
using (StreamReader confReader = File.OpenText ("/etc/resolv.conf")) {
StringReader sr = new StringReader (confReader.ReadToEnd ());
string line;
toWrite = "";
while (null != (line = sr.ReadLine ())) {
if (line.Contains ("nameserver")) {
Console.WriteLine (line);
} else {
toWrite += line + Environment.NewLine;
}
}
confReader.Dispose ();
confReader.Close ();
}
s1 = "nameserver " + DNSentry1.Text + ";" + Environment.NewLine;
s2 = "nameserver " + DNSentry2.Text + ";" + Environment.NewLine;
Console.WriteLine ("=============");
Console.WriteLine ("Writing to resolv.conf");
Console.WriteLine ("To write: " + toWrite + s1 + s2);
using (StreamWriter confWriter = new StreamWriter ("/etc/resolv.conf", false)) {
Console.WriteLine ("Writing...");
confWriter.Write (toWrite + s1 + s2);
Console.WriteLine ("Closing file stream...");
confWriter.Dispose ();
confWriter.Close ();
}
Console.WriteLine ("=============");
Console.WriteLine ("Opening conf to confirm if it worked");
if (IsLinux) {
Console.WriteLine ("Trying to open conf");
StreamReader file = File.OpenText ("/etc/resolv.conf");
string s = file.ReadToEnd ();
Console.WriteLine(s);
file.Dispose ();
file.Close ();
}
}
The relevant part is
using (StreamWriter confWriter = new StreamWriter ("/etc/resolv.conf", false)) {
Console.WriteLine ("Writing...");
confWriter.Write (toWrite + s1 + s2);
Console.WriteLine ("Closing file stream...");
confWriter.Dispose ();
confWriter.Close ();
}
Wherein I overwrite resolv.conf with the filled in DNS info. An example input to this would be something like "8.8.8.8" in the first dialog and "8.8.4.4" in the second. The output would be...
=============
Reading from resolv.conf before writing...
nameserver 192.168.2.101
nameserver 8.8.8.8
=============
Writing to resolv.conf
To write: # Generated by NetworkManager
domain trin-it.local
search trin-it.local
nameserver 8.8.8.8;
nameserver 8.8.4.4;
Writing...
Closing file stream...
=============
Opening conf to confirm if it worked
4
Trying to open conf
# Generated by NetworkManager
domain trin-it.local
search trin-it.local
nameserver 8.8.8.8;
nameserver 8.8.4.4;
If I ping google after this it just says: "unknown host google"
HOWEVER, if I manually go to resolv.conf and change the nameservers there it actually resolves just fine. What is up with that? The only change is that I do it through code instead of just nano /etc/resolv.conf, as far as I can tell. Can anyone shed some light on this?
TL;DR
Why would editing resolv.conf with code NOT work as opposed to manually editing it? Wouldn't it be the same thing?