Skip to content

Commit

Permalink
FirstCommit
Browse files Browse the repository at this point in the history
tsuttsu305 committed Feb 23, 2015
1 parent 89a2347 commit 2162f48
Showing 17 changed files with 1,717 additions and 0 deletions.
22 changes: 22 additions & 0 deletions EthSwitcher.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EthSwitcher", "EthSwitcher\EthSwitcher.csproj", "{AE1F1077-6F39-4892-9EAB-F106BF629D97}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AE1F1077-6F39-4892-9EAB-F106BF629D97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE1F1077-6F39-4892-9EAB-F106BF629D97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE1F1077-6F39-4892-9EAB-F106BF629D97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE1F1077-6F39-4892-9EAB-F106BF629D97}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions EthSwitcher/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
112 changes: 112 additions & 0 deletions EthSwitcher/ConfigList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace EthSwitcher {
public class ConfigList {
public class Config {
public String Name { get; set; }

public String IpAddress { get; set; }

public String Subnet { get; set; }

public String Gateway { get; set; }

public String Dns1 { get; set; }

public String Dns2 { get; set; }
}

private static readonly ConfigList Instance = new ConfigList();

private List<Config> _cfgList;

private ConfigList() {
Load();
}

public static ConfigList GetInstance() {
return Instance;
}


public List<Config> GetList() {
return _cfgList;
}

public void Add(String name, String ipAddress, String subnet, String gateway, String dns1, String dns2) {
var data = new Config();
data.Name = name;
data.IpAddress = ipAddress;
data.Subnet = subnet;
data.Gateway = gateway;
data.Dns1 = dns1;
data.Dns2 = dns2;

_cfgList.Add(data);
Save();
}

public void Add(Config data) {
_cfgList.Add(data);
Save();
}

public void Remove(Config cfg) {
_cfgList.Remove(cfg);
Save();
}

public Config GetItem(int index) {
try {
return _cfgList[index];
} catch (Exception) {
return null;
}
}

public String[] GetNames() {
if (_cfgList.Count == 0) return new[] {""};
String[] str = new string[_cfgList.Count];
for (int i = 0; i < _cfgList.Count; i++) {
str[i] = _cfgList[i].Name;
}
return str;
}

public class SaveData {
public List<Config> Data { get; set; }
}

public void Save() {
SaveData data = new SaveData();
data.Data = _cfgList;

XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveData));

FileStream outStream = new FileStream("settings.xml", FileMode.Create);
xmlSerializer.Serialize(outStream, data);
outStream.Close();
}

public void Load() {
try {
XmlSerializer xmlSerializer = new XmlSerializer(typeof (SaveData));

FileStream inStream = new FileStream("settings.xml", FileMode.Open);

SaveData data = (SaveData) xmlSerializer.Deserialize(inStream);
inStream.Close();

_cfgList = data.Data;
} catch (Exception) {
_cfgList = new List<Config>();
}
}
}
}
100 changes: 100 additions & 0 deletions EthSwitcher/EthSwitcher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AE1F1077-6F39-4892-9EAB-F106BF629D97}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EthSwitcher</RootNamespace>
<AssemblyName>EthSwitcher</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigList.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="NameDialog.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="NameDialog.Designer.cs">
<DependentUpon>NameDialog.cs</DependentUpon>
</Compile>
<Compile Include="NetworkAdapterController.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="NameDialog.resx">
<DependentUpon>NameDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
514 changes: 514 additions & 0 deletions EthSwitcher/Form1.Designer.cs

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions EthSwitcher/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EthSwitcher {
public partial class Form1 : Form {

private NetworkAdapterController _controller;
private List<String> _adapters;

public Form1() {
InitializeComponent();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
Application.Exit();
}

private void Form1_Load(object sender, EventArgs e) {
_controller = new NetworkAdapterController();

_adapters = _controller.GetAdapters();

nicList.DropDownStyle = ComboBoxStyle.DropDownList;
nicList.DataSource = _adapters;

settingList.DataSource = ConfigList.GetInstance().GetNames();
settingList.DisplayMember = "Name";
}

private void nicList_SelectedIndexChanged(object sender, EventArgs e) {
String nic = _adapters[nicList.SelectedIndex];

ipaddrView.Text = _controller.GetIPAddress(nic);
maskView.Text = _controller.GetSubnetMask(nic);
gatewayView.Text = _controller.GetGateway(nic);
String[] dns = _controller.GetDnsServers(nic);
dns1view.Text = dns[0];
dns2View.Text = dns[1];
macView.Text = _controller.GetMacAddress(nic);
}

private void setNowBtn_Click(object sender, EventArgs e) {
String nic = _adapters[nicList.SelectedIndex];
try {
IPAddress address;
if (String.IsNullOrWhiteSpace(ipaddrBox.Text) || (IPAddress.TryParse(ipaddrBox.Text, out address))) {
if (String.IsNullOrWhiteSpace(maskBox.Text) || IPAddress.TryParse(maskBox.Text, out address)) {
if (String.IsNullOrWhiteSpace(gatewayBox.Text) ||
(IPAddress.TryParse(gatewayBox.Text, out address))) {
if (String.IsNullOrWhiteSpace(dns1Box.Text) ||
(IPAddress.TryParse(dns1Box.Text, out address))) {
if (String.IsNullOrWhiteSpace(dns2Box.Text) ||
(IPAddress.TryParse(dns2Box.Text, out address))) {
_controller.SetDNSServers(nic, new string[] { dns1Box.Text, dns2Box.Text });
_controller.SetIPAddress(nic, ipaddrBox.Text, maskBox.Text);
_controller.SetGateway(nic, gatewayBox.Text);

ipaddrView.Text = _controller.GetIPAddress(nic);
maskView.Text = _controller.GetSubnetMask(nic);
gatewayView.Text = _controller.GetGateway(nic);
String[] dns = _controller.GetDnsServers(nic);
dns1view.Text = dns[0];
dns2View.Text = dns[1];
macView.Text = _controller.GetMacAddress(nic);
} else {
MessageBox.Show("DNS2が不正です。");
}
} else {
MessageBox.Show("DNS1が不正です。");
}
} else {
MessageBox.Show("Gatewayが不正です。");
}
} else {
MessageBox.Show("SubnetMaskが不正です。");
}
} else {
MessageBox.Show("IPアドレスが不正です。");
}
} catch (Exception) {
MessageBox.Show("未知のエラーが発生しました");
}
}

private void settingList_SelectedIndexChanged(object sender, EventArgs e) {
ConfigList.Config data = ConfigList.GetInstance().GetItem(settingList.SelectedIndex);

if (data == null)return;

ipaddrBox.Text = data.IpAddress;
maskBox.Text = data.Subnet;
gatewayBox.Text = data.Gateway;
dns1Box.Text = data.Dns1;
dns2Box.Text = data.Dns2;
nameBox.Text = data.Name;
}

private void addNowBtn_Click(object sender, EventArgs e) {
NameDialog nd = new NameDialog();
if (nd.ShowDialog(this) == DialogResult.OK) {
ConfigList.GetInstance().Add(nd.Name, ipaddrView.Text, maskView.Text, gatewayView.Text, dns1view.Text, dns2View.Text);
}
nd.Dispose();

settingList.DataSource = ConfigList.GetInstance().GetNames();
}

private void delBtn_Click(object sender, EventArgs e) {
ConfigList.Config data = ConfigList.GetInstance().GetItem(settingList.SelectedIndex);

if (data == null)return;

ConfigList.GetInstance().Remove(data);

settingList.DataSource = ConfigList.GetInstance().GetNames();
}

private void addListBtn_Click(object sender, EventArgs e) {
ConfigList.Config data = new ConfigList.Config();

data.Name = nameBox.Text;
data.IpAddress = ipaddrBox.Text;
data.Subnet = maskBox.Text;
data.Gateway = gatewayBox.Text;
data.Dns1 = dns1Box.Text;
data.Dns2 = dns2Box.Text;

ConfigList.GetInstance().Add(data);
settingList.DataSource = ConfigList.GetInstance().GetNames();
}
}
}
126 changes: 126 additions & 0 deletions EthSwitcher/Form1.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>141, 17</value>
</metadata>
</root>
83 changes: 83 additions & 0 deletions EthSwitcher/NameDialog.Designer.cs
25 changes: 25 additions & 0 deletions EthSwitcher/NameDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EthSwitcher {
public partial class NameDialog : Form {

public String Name;

public NameDialog() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
Name = textBox1.Text;
DialogResult = DialogResult.OK;
}
}
}
120 changes: 120 additions & 0 deletions EthSwitcher/NameDialog.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
202 changes: 202 additions & 0 deletions EthSwitcher/NetworkAdapterController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace EthSwitcher {
public class NetworkAdapterController {

private ManagementClass _managementClass;
private ManagementObjectCollection _nics;

public NetworkAdapterController() {
init();
}

public void init() {
_managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
_nics = _managementClass.GetInstances();
}

public List<String> GetAdapters() {
init();
List<String> list = new List<string>();
foreach (ManagementBaseObject nic in _nics) {
list.Add((string) nic["Caption"]);
}
return list;
}

public String GetIPAddress(String adapter) {
init();
String ip = "NoIPAddress";
foreach (ManagementBaseObject nic in _nics) {
if (nic["Caption"].Equals(adapter)) {
if ((bool) nic["IPEnabled"]) {
if ((bool) nic["DHCPEnabled"]) {
if (nic["IPAddress"] != null) {
ip = "DHCP:" + ((String[])nic["IPAddress"])[0];
break;
} else {
ip = "DHCP";
break;
}
} else if (nic["IPAddress"] != null) {
ip = ((String[]) nic["IPAddress"])[0];
break;
}
} else {
ip = "IP Not Active";
}
}
}
return ip;
}

public String GetSubnetMask(String adapter) {
init();
String mask = "";
foreach (ManagementBaseObject nic in _nics) {
if (nic["Caption"].Equals(adapter)) {
if ((bool)nic["IPEnabled"]) {
if (nic["IPSubnet"] != null) {
mask = ((String[]) nic["IPSubnet"])[0];
}
}

break;
}
}
return mask;
}

public String GetGateway(String adapter) {
init();
String gw = "";
foreach (ManagementBaseObject nic in _nics) {
if (nic["Caption"].Equals(adapter)) {
if ((bool)nic["IPEnabled"]) {
if (nic["DefaultIPGateway"] != null) {
gw = ((String[])nic["DefaultIPGateway"])[0];
}
}
break;
}
}
return gw;
}

public String[] GetDnsServers(String adapter) {
init();
String[] dns = {"", ""};
foreach (ManagementBaseObject nic in _nics) {
if (nic["Caption"].Equals(adapter)) {
if ((bool)nic["IPEnabled"]) {
if (nic["DNSServerSearchOrder"] != null) {
String[] d = (string[]) nic["DNSServerSearchOrder"];
dns[0] = d[0];
if (d.Count() >= 2) {
dns[1] = d[1];
}
}
}
break;
}
}
return dns;
}

public String GetMacAddress(String adapter) {
init();
String mac = "";
foreach (ManagementBaseObject nic in _nics) {
if (nic["Caption"].Equals(adapter)) {
mac = (string) nic["MACAddress"];
break;
}
}
return mac;
}

/*Adapter Setting*/
public bool SetIPAddress(String adapter, String ip_address, String subnet_mask) {
init();
if (String.IsNullOrWhiteSpace(ip_address) || String.IsNullOrWhiteSpace(subnet_mask)) {
foreach (var o in _nics) {
var nic = (ManagementObject) o;
if (nic["Caption"].Equals(adapter)) {
nic.InvokeMethod("EnableDHCP", null, null);
return false;
}
}
return true;
} else {
foreach (var o in _nics) {
var nic = (ManagementObject) o;
if (nic["Caption"].Equals(adapter)) {
var newIP = nic.GetMethodParameters("EnableStatic");

newIP["IPAddress"] = new[] {ip_address};
newIP["SubnetMask"] = new[] {subnet_mask};

nic.InvokeMethod("EnableStatic", newIP, null);
return true;
}
}
}
return false;
}

public bool SetGateway(String adapter, String gateway) {
init();
if (String.IsNullOrWhiteSpace(gateway)) {
gateway = null;
}
foreach (var o in _nics) {
var nic = (ManagementObject) o;
if (nic["Caption"].Equals(adapter)) {
var newGateway = nic.GetMethodParameters("SetGateways");
if (gateway != null) {
newGateway["DefaultIPGateway"] = new[] {gateway};
newGateway["GatewayCostMetric"] = new[] {1};
} else
{
newGateway = null;
}

nic.InvokeMethod("SetGateways", newGateway, null);
return true;
}
}
return false;
}

public bool SetDNSServers(String adapter, String[] dnsservers) {
init();
if (String.IsNullOrWhiteSpace(dnsservers[0])) {
dnsservers = null;
} else if(String.IsNullOrWhiteSpace(dnsservers[1])) {
dnsservers = new[] {dnsservers[0]};
}

foreach (var o in _nics) {
var nic = (ManagementObject) o;
if (nic["Caption"].Equals(adapter)) {
var newDNS = nic.GetMethodParameters("SetDNSServerSearchOrder");

if (dnsservers != null) {
newDNS["DNSServerSearchOrder"] = dnsservers;
} else {
newDNS["DNSServerSearchOrder"] = new string[]{};
}

nic.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
return true;
}
}
return false;
}
}
}
19 changes: 19 additions & 0 deletions EthSwitcher/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EthSwitcher {
static class Program {
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
36 changes: 36 additions & 0 deletions EthSwitcher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
// アセンブリに関連付けられている情報を変更するには、
// これらの属性値を変更してください。
[assembly: AssemblyTitle("EthSwitcher")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EthSwitcher")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから
// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、
// その型の ComVisible 属性を true に設定してください。
[assembly: ComVisible(false)]

// 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です
[assembly: Guid("2c9f35b8-83d5-4c8b-a661-db6ed42be2c4")]

// アセンブリのバージョン情報は、以下の 4 つの値で構成されています:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
62 changes: 62 additions & 0 deletions EthSwitcher/Properties/Resources.Designer.cs
117 changes: 117 additions & 0 deletions EthSwitcher/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
26 changes: 26 additions & 0 deletions EthSwitcher/Properties/Settings.Designer.cs
7 changes: 7 additions & 0 deletions EthSwitcher/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

0 comments on commit 2162f48

Please sign in to comment.