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.不想彈出該框的辦法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM