C# 修改IP地址与操作access
C# 修改IP地址,操作access
?
?使用说明
1. 系统必须安装.NET Framework 2.0 或更高的版本 (文件:dotnetfx2.0.exe)。
2. 将压缩包解压即可使用本程序。
3. 必须在管理员权限运行。
4. 如果系统安装杀毒软件或安全卫士,会弹出修改IP的提示,请记住选择,并允许执行。
5. 修改IP地址,必须让网卡连接上网络才能修改。
?
使用方法:
运行软件后,先配置方案,步骤:
? 1.点击软件界面右上角的 【方案配置】
? 2.安装页面添加方案,添加成功后,点击返回。
? 3.方案列表中就会出现刚才添加的新方案。
? 4.选择一个方案,然后选择网卡(如果只有一个网卡联网,可不用选择)
? 5.点击 【更改 IP 地址】 按钮
? 6.等待更改成功。
?
?
?
核心源码:?
?Conn.cs
using System;using System.Collections.Generic;using System.Text;using System.Management;using System.Collections;using Microsoft.Win32;using System.Net.NetworkInformation;using System.Runtime.InteropServices;namespace IPSwitch{ public class Conn { /// <summary> /// 新增按钮的名称 /// </summary> public const string _ADDED_NAME = "新增"; /// <summary> /// 修改按钮的名称 /// </summary> public const string _MODIFY_NAME = "编辑"; /// <summary> /// 重置按钮的名称 /// </summary> public const string _RESET_NAME = "重置"; /// <summary> /// 删除按钮的名称 /// </summary> public const string _DELETE_NAME = "删除"; /// <summary> /// 保存按钮的名称 /// </summary> public const string _SAVE_NAME = "保存"; /// <summary> /// 标题文字 /// </summary> public static string _TITLE = ""; /// <summary> /// 获取title内容 /// </summary> /// <returns></returns> public static string GetTitle() { _TITLE = DBReadAccess.GetObjectBySQL("select top 1 g_value from globals where g_name ='title'").ToString(); return _TITLE; } /// <summary> /// 实例化 Win32_NetworkAdapterConfiguration类 /// </summary> /// <returns></returns> private static ManagementObjectCollection GetMCInstances() { ManagementClass processClass = new ManagementClass("Win32_NetworkAdapterConfiguration"); return processClass.GetInstances(); } /// <summary> /// 获取当前系统中所有网卡的名称 /// </summary> /// <returns></returns> public static List<string> GetNICNameList() { List<string> list = new List<string>(); ManagementObjectCollection moc = Conn.GetMCInstances(); //获取当前系统的所有NIC foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { //将当前已经启动的网卡名称加以标识 //list.Insert(0,mo.GetPropertyValue("Description").ToString()); list.Add(mo.GetPropertyValue("SettingID").ToString()); continue; } //list.Add("(不可用)"+mo.GetPropertyValue("Description").ToString()); } return GetNICNameList(list); } /// <summary> /// 获取当前可以用网卡 /// </summary> /// <returns></returns> public static int GetIPEnabledNum() { int i = 0; ManagementObjectCollection moc = Conn.GetMCInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { i++; } } return i; } /// <summary> /// 设置 自动获取IP /// </summary> public static void SetIpAddressAuto() { return; //ManagementObjectCollection moc = Conn.GetMCInstances(); //ManagementBaseObject inPar = null; //foreach (ManagementObject mo in moc) //{ // if (!(bool)mo["IPEnabled"]) // { // //如果网卡未启动就直接跳过 // continue; // } // //设置注册表的内容 // string sid = mo.GetPropertyValue("SettingID").ToString(); // RegistryKey rk = EditReg(sid); // if (rk != null) // { // rk.SetValue("IPAddress", "0.0.0.0"); // rk.SetValue("SubnetMask", ""); // rk.SetValue("DefaultGateway", ""); // rk.SetValue("NameServer", ""); // rk.SetValue("EnableDHCP", "dword:00000001");//自动 // //Convert.ToInt16(1); // } // //设置IP 自动获取 // mo.InvokeMethod("EnableDHCP", null, null); // //设置DNS 自动获取 // inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); // inPar["DNSServerSearchOrder"] = null; // mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); //} } /// <summary> /// 修改IP地址 /// </summary> /// <param name="nic"></param> /// <param name="ip"></param> /// <param name="mask"></param> /// <param name="way"></param> /// <param name="dns"></param> public static void SetIpAddress(string nic, string[] ip, string[] mask, string[] way, string[] dns) { nic = GetNICId(nic); ManagementObjectCollection moc = Conn.GetMCInstances(); ManagementBaseObject inPar = null; foreach (ManagementObject mo in moc) { string nicid = mo.GetPropertyValue("SettingID").ToString(); //当前网卡设置IP,其他网卡设置自动获取 if (nicid.Equals(nic)) { //IP 地址设置 if (ip != null && mask != null && way != null) { //设置IP地址 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = ip; inPar["SubnetMask"] = mask; mo.InvokeMethod("EnableStatic", inPar, null); //设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = way; mo.InvokeMethod("SetGateways", inPar, null); //设置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = dns; mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); return; } } ////设置IP 自动获取 //mo.InvokeMethod("EnableDHCP", null, null); ////设置DNS 自动获取 //inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); //inPar["DNSServerSearchOrder"] = null; //mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); //设置注册表的内容 RegistryKey rk = EditReg(nicid); if (rk != null) { //rk.DeleteSubKey("DisableDhcpOnConnect"); try { rk.DeleteValue("DisableDhcpOnConnect"); } catch { } try { rk.SetValue("EnableDHCP", "1", RegistryValueKind.DWord); } catch { } try { rk.DeleteValue("NameServer"); } catch { } try { rk.SetValue("IPAddress", "0.0.0.0"); } catch { } try { rk.DeleteValue("SubnetMask"); } catch { } try { rk.DeleteValue("DefaultGateway"); } catch { } //rk.Flush(); rk.Close(); } } } /// <summary> /// 加载网卡ID /// </summary> /// <param name="names"></param> /// <returns></returns> public static string GetNICId(string nicname) { string name = null; NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.Name.Equals(nicname)) { name = adapter.Id; } } return name; } /// <summary> /// 加载网卡名称 /// </summary> /// <param name="names"></param> /// <returns></returns> public static List<string> GetNICNameList(List<string> names) { List<string> list = new List<string>(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (names.Contains(adapter.Id)) { list.Insert(0, adapter.Name); } else { list.Add("(不可用)" + adapter.Name); } } return list; } /// <summary> /// 获取对应的注册表项 /// </summary> /// <param name="subkey"></param> /// <returns></returns> public static RegistryKey EditReg(string subkey) { try { RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces"); return rk.OpenSubKey(subkey,true); } catch { return null; } //本地 {4D5423C7-F885-4787-A1FA-18CB41BDEFC5} //无线 {B74AB920-C254-4FF3-9B49-2F495DD8079B} //rk.GetValue(""); //rk.SetValue("",""); } }}
??
?
?
DBReadAccess.cs:
using System;using System.Collections.Generic;using System.Text;using System.Data.OleDb;using System.Data;using System.Configuration;namespace IPSwitch{ /// <summary> /// 处理access的类 /// </summary> public class DBReadAccess { /// <summary> /// Access 的数据库连接字符串. /// </summary> private static readonly string connString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; //@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb;Jet OLEDB:Database Password=switch"; private static OleDbConnection conn = null; /// <summary> /// 单例 返回连接对象 /// </summary> /// <returns></returns> private static OleDbConnection GetConn() { if (conn == null) { //connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString(); conn = new OleDbConnection(connString);// 建立数据库连接. } return conn; } private static void isopen() { // 初始化实例 DBReadAccess.GetConn(); if (conn.State != ConnectionState.Open) { conn.Open(); } } /// <summary> /// 根据sql语句,获取datatable /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable GetDataTableBySQL(string sql) { isopen(); DataTable dt = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);// 创建一个适配器 adapter.Fill(dt);//绑定table adapter.Dispose(); conn.Close();// 关闭数据库连接. return dt; } /// <summary> /// 根据sql语句查询第一行第一列的值 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object GetObjectBySQL(string sql) { isopen(); OleDbCommand cmd = conn.CreateCommand();// 创建一个 Command. cmd.CommandText = sql;// 定义需要执行的SQL语句. object obj = cmd.ExecuteScalar();//查询第一行第一列 cmd.Dispose(); conn.Close();// 关闭数据库连接. return obj; } /// <summary> /// 根据sql语句执行更新语句 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int GetExecuteBySQL(string sql) { isopen(); OleDbCommand cmd = conn.CreateCommand();// 创建一个 Command. cmd.CommandText = sql;// 定义需要执行的SQL语句. int num = cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close();// 关闭数据库连接. return num; } }}
?
?
更多的页面操作请下载附件查看源码。
?
?
?
?
?
?