本帖最后由 icebear2 于 2009-07-13 16:27:52 编辑

解决方案 »

  1.   

     static void SetNetworkAdapter() 
            { 
                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; 
     
                    //设置ip地址和子网掩码 
                    inPar = mo.GetMethodParameters( "EnableStatic" ); 
                    inPar["IPAddress"] = new string[] { "10.22.21.111","192.168.10.9" }; 
                    inPar["SubnetMask"] = new string[] { "255.255.255.0","255.255.255.0" }; 
                    outPar = mo.InvokeMethod( "EnableStatic", inPar, null ); 
     
                    //设置网关地址 
                    inPar = mo.GetMethodParameters("SetGateways"); 
                    inPar["DefaultIPGateway"] = new string[] { "10.22.21.1","192.168.10.1"}; 
                    outPar = mo.InvokeMethod( "SetGateways", inPar, null ); 
     
                    //设置DNS 
                    inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); 
                    inPar["DNSServerSearchOrder"] = new string[] {"179.32.42.4","179.32.42.5"}; 
                    outPar = mo.InvokeMethod( "SetDNSServerSearchOrder" ,inPar,null); 
                        break; 
                } 
            } 
      

  2.   

    楼主说不用WMI哦上面的不行吧!期待中最好有牛人自己写一个组件出来哦!好好学
      

  3.   

    不用使用WMI,下面这样也可以。
    //获取本地连接信息
    NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
                {
                    if(NetworkIntf.NetworkInterfaceType ==NetworkInterfaceType.Ethernet )              
                            comboBox1.Items.Add(NetworkIntf.Name);               
                }
    //如果有网卡,默认选中第一块网卡
                if (comboBox1.Items.Count > 0)
                {
                    comboBox1.SelectedIndex = 0;
                    GetNetInfor(comboBox1.SelectedItem.ToString());
                } public void GetNetInfor(string strNetAdapterName)
            {            
                //获取选中的网卡的IP,网关,子网掩码,DNS,备用DNS信息            
                NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
                {
                    if (NetworkIntf.Name == strNetAdapterName)
                    {
                        IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
                        //获取本机IP地址信息,子网掩码信息
                        UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
                        if (UnicastIPAddressInformationCollection.Count > 0)
                        {
                            textBox1.Text = UnicastIPAddressInformationCollection[0].Address.ToString();
                            textBox2.Text = UnicastIPAddressInformationCollection[0].IPv4Mask.ToString();
                        }
                        else
                        {
                            textBox1.Text = "0.0.0.0";
                            textBox2.Text = "0.0.0.0";
                        }
                        //获取当前网关设置
                        GatewayIPAddressInformationCollection   GWAddress = IPInterfaceProperties.GatewayAddresses;
                        if (GWAddress.Count > 0)
                            textBox3.Text = GWAddress[0].Address.ToString();
                        else
                            textBox3.Text = "0.0.0.0";
                        //获取当前DNS设置
                        IPAddressCollection DNSAddress = IPInterfaceProperties.DnsAddresses;
                        switch (DNSAddress.Count )
                        {
                            case 1:
                                textBox4.Text = DNSAddress[0].ToString();
                                textBox5.Text = "0.0.0.0";
                                break;
                            case 2:                        
                                textBox4.Text = DNSAddress[0].ToString();
                                textBox5.Text = DNSAddress[1].ToString();
                                break;
                            default :
                                textBox4.Text = "0.0.0.0";
                                textBox5.Text = "0.0.0.0";
                                break;                       
                        }
                    }
                }
            }
      

  4.   

    修改
    自动获取
    private ManagementBaseObject iObj = null;
            private ManagementBaseObject oObj = null;
            private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            private readonly ManagementObjectCollection moc;
            public void EnableDHCP()
            {
                foreach (ManagementObject mo in moc)
                {
                    if (!(bool)mo["IPEnabled"]) continue;
                    if (!(bool)mo["DHCPEnabled"])
                    {
                        iObj = mo.GetMethodParameters("EnableDHCP");
                        oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
                    }
                }
            }
       public Form1()
            {
                InitializeComponent();
                moc = mc.GetInstances();
            }
            private void button3_Click(object sender, EventArgs e)
            {
                EnableDHCP();
            }另外用cmd命令自动获取private void button1_Click(object sender, EventArgs e)
            {
                string a = "set   address     name   =\"本地连接\"   source   =   dhcp";
                string b = "set   dns   name   =\"本地连接\"   source   =   dhcp";
                string c = "set   wins   name   =\"本地连接\"   source   =   dhcp";            Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();            p.StandardInput.WriteLine("netsh");
                p.StandardInput.WriteLine("interface");
                p.StandardInput.WriteLine("ip");
                p.StandardInput.WriteLine(a.ToString());
                p.StandardInput.WriteLine(b.ToString());
                p.StandardInput.WriteLine(c.ToString());
                p.StandardInput.WriteLine("exit");
                p.StandardInput.WriteLine("exit");
                p.Close();            
            }
      

  5.   

    这句有错误moc = mc.GetInstances();未处理 System.Management.ManagementException
      Message="找不到 "
      Source="System.Management"
      StackTrace:
           在 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
           在 System.Management.ManagementObject.Initialize(Boolean getObject)
           在 System.Management.ManagementBaseObject.get_wbemObject()
           在 System.Management.ManagementBaseObject.get_ClassName()
           在 System.Management.ManagementClass.GetInstances(EnumerationOptions options)
           在 System.Management.ManagementClass.GetInstances()
           在 ChangeIp.Form1..ctor() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Form1.cs:行号 21
           在 ChangeIp.Program.Main() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  6.   

    用批的话就这样
    @echo off
    set /p a=请输入所要更改的IP
    set /p b=请输入网关
    netsh int ip set address %a% static %b% 1
    pause>nul