Skip to content

Commit

Permalink
* some bugfixes for configuration loading
Browse files Browse the repository at this point in the history
* fixed a bug that would always load a previous version config file
* added setting field to keep track of configuration upgrade from previous versions
  • Loading branch information
tryallthethings committed May 26, 2023
1 parent 19ea80e commit f67c1b3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 37 deletions.
9 changes: 6 additions & 3 deletions DNS-Swapper/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
<userSettings>
<DNS_Swapper.Properties.Settings>
<setting name="NIC" serializeAs="String">
<value/>
<value />
</setting>
<setting name="DNS_1" serializeAs="String">
<value/>
<value />
</setting>
<setting name="DNS_2" serializeAs="String">
<value/>
<value />
</setting>
<setting name="WindowLocation" serializeAs="String">
<value>0, 0</value>
</setting>
<setting name="LastUpdatedVersion" serializeAs="String">
<value />
</setting>
</DNS_Swapper.Properties.Settings>
</userSettings>
</configuration>
77 changes: 47 additions & 30 deletions DNS-Swapper/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace DNS_Swapper
public partial class MainMenu : Form
{
public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
private bool updateGUI = false;

public MainMenu()
{
Expand All @@ -25,7 +26,12 @@ public MainMenu()
InitializeComponent();

// Upgrade settings file (user.config in %LOCALAPPDATA%\DNS_Swapper from previous version
Settings.Default.Upgrade();
if (Settings.Default.LastUpdatedVersion != Version)
{
Settings.Default.Upgrade();
Settings.Default.LastUpdatedVersion = Version;
Settings.Default.Save();
}

// Load network interfaces
ScanNICs();
Expand Down Expand Up @@ -241,9 +247,11 @@ private void CallSwapDNS(string NIC, string DNS)

private void ResetToolStripMenuItem_Click(object sender, EventArgs e)
{
updateGUI = true;
NIC_select.SelectedIndex = -1;
DNS_1.Text = "";
DNS_2.Text = "";
updateGUI = false;
}

private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
Expand All @@ -257,11 +265,14 @@ private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
Application.Exit();
}

private void SaveSettings()
private void SaveSettings(bool NIConly = false)
{
// Check for placeholder values and save accordingly
Settings.Default.DNS_1 = DNS_1.Text != "..." ? DNS_1.Text : "";
Settings.Default.DNS_2 = DNS_2.Text != "..." ? DNS_2.Text : "";
if (!NIConly)
{
// Check for placeholder values and save accordingly
Settings.Default.DNS_1 = DNS_1.Text != "..." ? DNS_1.Text : "";
Settings.Default.DNS_2 = DNS_2.Text != "..." ? DNS_2.Text : "";
}

// Check if NIC_select has a selected item
if (NIC_select.SelectedItem != null)
Expand Down Expand Up @@ -295,6 +306,10 @@ private void TaskBarIcon_Click(object sender, MouseEventArgs e)

private void UpdateIP(object sender, EventArgs e)
{
if(updateGUI)
{
return;
}
bool IPv4IPfound = false;
bool IPv6IPfound = false;
bool IPv4GWfound = false;
Expand All @@ -303,36 +318,36 @@ private void UpdateIP(object sender, EventArgs e)
ComboboxItem selectedNic = (ComboboxItem)NIC_select.SelectedItem;
NetworkInterface nic = (NetworkInterface)selectedNic.Value;

foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPv4IPfound = true;
IPv4_Text.Text = ip.Address.ToString();
foreach (GatewayIPAddressInformation gwipv4 in nic.GetIPProperties().GatewayAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
if (gwipv4.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPv4IPfound = true;
IPv4_Text.Text = ip.Address.ToString();
foreach (GatewayIPAddressInformation gwipv4 in nic.GetIPProperties().GatewayAddresses)
{
if (gwipv4.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPv4GWfound = true;
IPv4_GW_Text.Text = gwipv4.Address.ToString();
}
}
IPv4GWfound = true;
IPv4_GW_Text.Text = gwipv4.Address.ToString();
}
else if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
}
}
else if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
IPv6IPfound = true;
IPv6_Text.Text = ip.Address.ToString();
foreach (GatewayIPAddressInformation gwipv6 in nic.GetIPProperties().GatewayAddresses)
{
if (gwipv6.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
IPv6IPfound = true;
IPv6_Text.Text = ip.Address.ToString();
foreach (GatewayIPAddressInformation gwipv6 in nic.GetIPProperties().GatewayAddresses)
{
if (gwipv6.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
IPv6GWfound = true;
IPv6_GW_Text.Text = gwipv6.Address.ToString();
}
}
IPv6GWfound = true;
IPv6_GW_Text.Text = gwipv6.Address.ToString();
}

}
}
}

if (!IPv4IPfound)
{
IPv4_Text.Text = "No IPv4 IP found";
Expand All @@ -349,6 +364,8 @@ private void UpdateIP(object sender, EventArgs e)
{
IPv6_GW_Text.Text = "No IPv6 Gateway found";
}

SaveSettings(true);
}

private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
Expand All @@ -360,7 +377,7 @@ private void AboutToolStripMenuItem_Click(object sender, EventArgs e)

private void ValidateIPField(object sender, EventArgs e)
{
var mb = (MaskedTextBox)sender;
var mb = (IPAddressControlLib.IPAddressControl)sender;
string ip = Regex.Replace(mb.Text, @"\s+", "");

if (ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length == 4)
Expand Down
4 changes: 2 additions & 2 deletions DNS-Swapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.7.0")]
[assembly: AssemblyFileVersion("1.5.7.0")]
[assembly: AssemblyVersion("1.5.9.0")]
[assembly: AssemblyFileVersion("1.5.9.0")]
12 changes: 12 additions & 0 deletions DNS-Swapper/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DNS-Swapper/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<Setting Name="WindowLocation" Type="System.Drawing.Point" Scope="User">
<Value Profile="(Default)">0, 0</Value>
</Setting>
<Setting Name="LastUpdatedVersion" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
4 changes: 2 additions & 2 deletions Installer_advinst/Installer_advinst.aip
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<ROW Property="MSIFASTINSTALL" MultiBuildValue="DefaultBuild:1"/>
<ROW Property="Manufacturer" Value="TryAllTheThings"/>
<ROW Property="MsiLogging" MultiBuildValue="DefaultBuild:vp"/>
<ROW Property="ProductCode" Value="1033:{F5131366-F09E-4EBF-8A4F-F15D4D3D4B74} " Type="16"/>
<ROW Property="ProductCode" Value="1033:{7E28E9A7-A37C-4FC7-9339-B5CCC4DD3D66} " Type="16"/>
<ROW Property="ProductLanguage" Value="1033"/>
<ROW Property="ProductName" Value="DNS-Swapper"/>
<ROW Property="ProductVersion" Value="1.5.7.0" Options="32"/>
<ROW Property="ProductVersion" Value="1.5.9.0" Options="32"/>
<ROW Property="REBOOT" MultiBuildValue="DefaultBuild:ReallySuppress"/>
<ROW Property="RUNAPPLICATION" Value="1" Type="4"/>
<ROW Property="SecureCustomProperties" Value="OLDPRODUCTS;AI_NEWERPRODUCTFOUND"/>
Expand Down

0 comments on commit f67c1b3

Please sign in to comment.