94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
|
|
namespace EthernetSwitcher
|
|
{
|
|
public enum EthernetMode
|
|
{
|
|
Auto,
|
|
Primary,
|
|
Secondary,
|
|
Disabled
|
|
}
|
|
|
|
public enum IpProtocol
|
|
{
|
|
IPv4,
|
|
IPv6,
|
|
IPv4_IPv6
|
|
}
|
|
|
|
public class Settings
|
|
{
|
|
private static readonly string SettingsFilePath = Path.Combine(Application.StartupPath, "settings.ini");
|
|
|
|
public EthernetMode Mode { get; set; } = EthernetMode.Auto;
|
|
public string PrimaryName { get; set; } = "";
|
|
public string SecondaryName { get; set; } = "";
|
|
public int CheckInterval { get; set; } = 5;
|
|
public IpProtocol Protocol { get; set; } = IpProtocol.IPv4_IPv6;
|
|
|
|
public static Settings Load()
|
|
{
|
|
var settings = new Settings();
|
|
|
|
if (File.Exists(SettingsFilePath))
|
|
{
|
|
var mode = ReadIniValue("Settings", "Mode", "Auto");
|
|
EthernetMode parsedMode;
|
|
if (Enum.TryParse<EthernetMode>(mode, out parsedMode))
|
|
{
|
|
settings.Mode = parsedMode;
|
|
}
|
|
|
|
settings.PrimaryName = ReadIniValue("Settings", "PrimaryName", "");
|
|
settings.SecondaryName = ReadIniValue("Settings", "SecondaryName", "");
|
|
|
|
var interval = ReadIniValue("Settings", "CheckInterval", "5");
|
|
int parsedInterval;
|
|
if (int.TryParse(interval, out parsedInterval))
|
|
{
|
|
settings.CheckInterval = parsedInterval;
|
|
}
|
|
|
|
var protocol = ReadIniValue("Settings", "Protocol", "IPv4_IPv6");
|
|
IpProtocol parsedProtocol;
|
|
if (Enum.TryParse<IpProtocol>(protocol, out parsedProtocol))
|
|
{
|
|
settings.Protocol = parsedProtocol;
|
|
}
|
|
}
|
|
|
|
return settings;
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
WriteIniValue("Settings", "Mode", Mode.ToString());
|
|
WriteIniValue("Settings", "PrimaryName", PrimaryName);
|
|
WriteIniValue("Settings", "SecondaryName", SecondaryName);
|
|
WriteIniValue("Settings", "CheckInterval", CheckInterval.ToString());
|
|
WriteIniValue("Settings", "Protocol", Protocol.ToString());
|
|
}
|
|
|
|
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
|
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
|
|
|
|
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
|
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, string returnedString, int size, string filePath);
|
|
|
|
private static void WriteIniValue(string section, string key, string value)
|
|
{
|
|
WritePrivateProfileString(section, key, value, SettingsFilePath);
|
|
}
|
|
|
|
private static string ReadIniValue(string section, string key, string defaultValue)
|
|
{
|
|
var buffer = new string('\0', 256);
|
|
var length = GetPrivateProfileString(section, key, defaultValue, buffer, buffer.Length, SettingsFilePath);
|
|
return buffer.Substring(0, length);
|
|
}
|
|
}
|
|
} |