C# 修改本机IP

1.配置程序权限为管理员权限运行,否则不会修改ip也不会报错
using System.Management;//需要引用的包

static void Main(string[] args)
        {
            //获得当前登录的Windows用户标示
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            //判断当前登录用户是否为管理员
            if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
            {
                //如果是管理员,则直接运行
                Console.WriteLine("直接启动");
                AllNetIp.SetNetworkAdapter("172.168.52.200", "255.255.255.0", "172.168.52.1");//AllNetIp 是类名称
            }
            else
            {
                //创建启动对象
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = "XXXX.exe";// Application.ExecutablePath;
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                try
                {
                    Console.WriteLine("重新已管理员身份启动");
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return;
                }
            }

        }
2.SetNetworkAdapter方法
        /// <summary>
        /// 修改本机IP
        /// </summary>
        public static void SetNetworkAdapter(string ip,string mask,string host)
        {
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;
                string[] addresses = (string[])mo["IPAddress"];
                if (addresses[0].Contains("xxx.xxx.xxx.xxx"))
                {
                    Console.WriteLine("不修改保护的地址");
                    continue;
                }
                Console.WriteLine("开始修改");
                try
                {
                    //设置ip地址和子网掩码 
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = new string[] { ip };
                    inPar["SubnetMask"] = new string[] { mask };//"255.255.255.0"
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                    //设置网关地址 
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = new string[] { host };
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                //设置DNS 
                //inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                //inPar["DNSServerSearchOrder"] = new string[] { "179.32.42.4", "179.32.42.5" };
                //outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                //break;
            }
        }
3.此时系统可能会弹出确认框

4.不想弹出该框的办法

posted @ 2022-03-25 15:38  8848-自律即自由  阅读(1570)  评论(1编辑  收藏  举报