mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-05 22:51:37 +12:00
Added dns change error messages
This commit is contained in:
parent
4eefd960ad
commit
6a15f8d5d3
|
@ -81,40 +81,29 @@ public string[] GetDNS()
|
||||||
return (string[])adapter["DnsServerSearchOrder"];
|
return (string[])adapter["DnsServerSearchOrder"];
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetDNS(string primary, string secondary)
|
public uint SetDNS(string primary, string secondary)
|
||||||
{
|
{
|
||||||
try
|
using (ManagementBaseObject parameters = adapter.GetMethodParameters("SetDNSServerSearchOrder"))
|
||||||
{
|
{
|
||||||
using (ManagementBaseObject parameters = adapter.GetMethodParameters("SetDNSServerSearchOrder"))
|
if (primary == null || secondary == null)
|
||||||
{
|
{
|
||||||
if (parameters != null)
|
// Obtain DNS server address automatically
|
||||||
{
|
parameters["DNSServerSearchOrder"] = null;
|
||||||
if (primary == null || secondary == null)
|
}
|
||||||
{
|
else
|
||||||
// Obtain DNS server address automatically
|
{
|
||||||
parameters["DNSServerSearchOrder"] = null;
|
parameters["DNSServerSearchOrder"] = new string[] { primary, secondary };
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
parameters["DNSServerSearchOrder"] = new string[] { primary, secondary };
|
|
||||||
}
|
|
||||||
|
|
||||||
using (ManagementBaseObject result = adapter.InvokeMethod("SetDNSServerSearchOrder", parameters, null))
|
// http://msdn.microsoft.com/en-us/library/aa393295(v=vs.85).aspx
|
||||||
{
|
using (ManagementBaseObject result = adapter.InvokeMethod("SetDNSServerSearchOrder", parameters, null))
|
||||||
return (uint)result["ReturnValue"] == 0;
|
{
|
||||||
}
|
return (uint)result["ReturnValue"];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
DebugHelper.WriteException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetDNSAutomatic()
|
public uint SetDNSAutomatic()
|
||||||
{
|
{
|
||||||
return SetDNS(null, null);
|
return SetDNS(null, null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,27 +129,46 @@ private void btnSave_Click(object sender, EventArgs e)
|
||||||
|
|
||||||
if (adapter != null)
|
if (adapter != null)
|
||||||
{
|
{
|
||||||
bool result = false;
|
uint result;
|
||||||
|
|
||||||
if (cbAutomatic.Checked)
|
try
|
||||||
{
|
{
|
||||||
result = adapter.SetDNSAutomatic();
|
if (cbAutomatic.Checked)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string primaryDNS = txtPreferredDNS.Text.Trim();
|
|
||||||
string secondaryDNS = txtAlternateDNS.Text.Trim();
|
|
||||||
|
|
||||||
if (Helpers.IsValidIPAddress(primaryDNS) && Helpers.IsValidIPAddress(secondaryDNS))
|
|
||||||
{
|
{
|
||||||
result = adapter.SetDNS(primaryDNS, secondaryDNS);
|
result = adapter.SetDNSAutomatic();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string primaryDNS = txtPreferredDNS.Text.Trim();
|
||||||
|
string secondaryDNS = txtAlternateDNS.Text.Trim();
|
||||||
|
|
||||||
|
if (Helpers.IsValidIPAddress(primaryDNS) && Helpers.IsValidIPAddress(secondaryDNS))
|
||||||
|
{
|
||||||
|
result = adapter.SetDNS(primaryDNS, secondaryDNS);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Not valid ip address.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == 0)
|
||||||
|
{
|
||||||
|
NativeMethods.DnsFlushResolverCache();
|
||||||
|
MessageBox.Show("DNS successfully set.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
}
|
||||||
|
else if (result == 1)
|
||||||
|
{
|
||||||
|
MessageBox.Show("DNS successfully set. Reboot is required.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
}
|
||||||
|
else if (result > 1)
|
||||||
|
{
|
||||||
|
MessageBox.Show("DNS set failed with error code: " + result, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
if (result)
|
|
||||||
{
|
{
|
||||||
NativeMethods.DnsFlushResolverCache();
|
MessageBox.Show("DNS set failed.\r\n" + ex.ToString(), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
MessageBox.Show("DNS successfully set.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue