C#设置本地网络(DNS、网关、子网掩码、IP)
C#设置本地网络(DNS、网关、子网掩码、IP)
如今网络在我们的生活工作中所起的作用越来越大,可以说离开了网络我们就无法正常的工作和生活。作为程序员我们写的程序大多数也会跟网络相关,而想要使用网络首先要将机器的网络配置设置好。而手动设置的方法显然很不可取,所以我们要让程序帮我们完成。下面是一个很常用的C#设置系统各种网络参数的一个小Demo一起看看吧。
这个Demo是通过"Win32_NetworkAdapterConfiguration"这个管理类.这里面已基本包括了IP,DNS,网关的设置信息。
在C#中使用WMI还是比较简单的:
using System; using System.Collections.Generic; using System.Text; using System; using System.Collections; using System.Text; using System.Management; using System.Text.RegularExpressions; namespace Demo { /// <summary> /// 网络设置类,设置网络的各种参数(DNS、网关、子网掩码、IP) /// </summary> public class NetworkSetting { public NetworkSetting() { // 构造函数逻辑 } /// <summary> /// 设置DNS /// </summary> /// <param name="dns"></param> public static void SetDNS( string [] dns) { SetIPAddress( null , null , null , dns); } /// <summary> /// 设置网关 /// </summary> /// <param name="getway"></param> public static void SetGetWay( string getway) { SetIPAddress( null , null , new string [] { getway }, null ); } /// <summary> /// 设置网关 /// </summary> /// <param name="getway"></param> public static void SetGetWay( string [] getway) { SetIPAddress( null , null , getway, null ); } /// <summary> /// 设置IP地址和掩码 /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> public static void SetIPAddress( string ip, string submask) { SetIPAddress( new string [] { ip }, new string [] { submask }, null , null ); } /// <summary> /// 设置IP地址,掩码和网关 /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> /// <param name="getway"></param> public static void SetIPAddress( string ip, string submask, string getway) { SetIPAddress( new string [] { ip }, new string [] { submask }, new string [] { getway }, null ); } /// <summary> /// 设置IP地址,掩码,网关和DNS /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> /// <param name="getway"></param> /// <param name="dns"></param> public static void SetIPAddress( string [] ip, string [] submask, string [] getway, string [] dns) { ManagementClass wmi = new ManagementClass( " Win32_NetworkAdapterConfiguration " ); ManagementObjectCollection moc = wmi.GetInstances(); ManagementBaseObject inPar = null ; ManagementBaseObject outPar = null ; foreach (ManagementObject mo in moc) { // 如果没有启用IP设置的网络设备则跳过 if (!( bool ) mo[ " IPEnabled " ]) continue ; // 设置IP地址和掩码 if (ip != null && submask != null ) { inPar = mo.GetMethodParameters( " EnableStatic " ); inPar[ " IPAddress " ] = ip; inPar[ " SubnetMask " ] = submask; outPar = mo.InvokeMethod( " EnableStatic " , inPar, null ); } // 设置网关地址 if (getway != null ) { inPar = mo.GetMethodParameters( " SetGateways " ); inPar[ " DefaultIPGateway " ] = getway; outPar = mo.InvokeMethod( " SetGateways " , inPar, null ); } // 设置DNS地址 if (dns != null ) { inPar = mo.GetMethodParameters( " SetDNSServerSearchOrder " ); inPar[ " DNSServerSearchOrder " ] = dns; outPar = mo.InvokeMethod( " SetDNSServerSearchOrder " , inPar, null ); } } } /// <summary> /// 启用DHCP服务器 /// </summary> public static void EnableDHCP() { ManagementClass wmi = new ManagementClass( " Win32_NetworkAdapterConfiguration " ); ManagementObjectCollection moc = wmi.GetInstances(); foreach (ManagementObject mo in moc) { // 如果没有启用IP设置的网络设备则跳过 if (!( bool ) mo[ " IPEnabled " ]) continue ; // 重置DNS为空 mo.InvokeMethod( " SetDNSServerSearchOrder " , null ); // 开启DHCP mo.InvokeMethod( " EnableDHCP " , null ); } } /// <summary> /// 判断是否符合IP地址格式 /// </summary> /// <param name="ip"></param> /// <returns></returns> public static bool IsIPAddress( string ip) { // 将完整的IP以“.”为界限分组 string [] arr = ip.Split( ' . ' ); // 判断IP是否为四组数组成 if (arr.Length != 4 ) return false ; // 正则表达式,1~3位整数 string pattern = @" \d{1,3} " ; for ( int i = 0 ; i < arr.Length; i++ ) { string d = arr[i]; // 判断IP开头是否为0 if (i == 0 && d == " 0 " ) return false ; // 判断IP是否是由1~3位数组成 if (! Regex.IsMatch(d, pattern)) return false ; if (d != " 0 " ) { // 判断IP的每组数是否全为0 d = d.TrimStart( ' 0 ' ); if (d == "" ) return false ; // 判断IP每组数是否大于255 if ( int .Parse(d) > 255 ) return false ; } } return true ; } } }
好了,写好上面这个类以后,就等着哪里需要然后NEW一个就可以了。很简单吧,如果遇到设置失败的情况,可能是因为权限不够,请参考 C#默认以管理员身份运行程序
欢迎大家光临我的CSDN博客
分类: .Net
C#默认以管理员身份运行程序
上篇博客写了一下如何通过网络时间更新系统时间,当时写的时候怎么测试都不成功,后来想想是不是我操作系统(当时是在win8上开发的)的问题。当时我猜应该是权限不够,导致无法修改系统时间,于是我以管理员身份运行了一次,结果测试成功!原来真的是权限的问题,于是就在程序里面加入了默认以管理员身份运行的代码。下面让我们看看是怎么实现的吧!
程序默认以管理员身份运行
static void Main( string [] Args) { /* * * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ // 获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); // 创建Windows用户主题 Application.EnableVisualStyles(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); // 判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { // 如果是管理员,则直接运行 Application.EnableVisualStyles(); Application.Run( new Form1()); } else { // 创建启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); // 设置运行文件 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; // 设置启动参数 startInfo.Arguments = String.Join( " " , Args); // 设置启动动作,确保以管理员身份运行 startInfo.Verb = " runas " ; // 如果不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); // 退出 System.Windows.Forms.Application.Exit(); } }
打开程序集里的Program.cs文件,并将其中Main方法中的代码替换为以上代码即可实现程序默认以管理员身份运行。
这篇博客本来早就应该发表的,可是由于网络等各种原因,一直到了现在才得以发表,网络问题很快就会得到解决,到时会继续恢复到每周一篇的更新频率,希望大家继续关注。
欢迎大家光临我的CSDN博客
分类: .Net
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于C#设置本地网络(DNS、网关、子网掩码、IP)的详细内容...