2009-03-27 22 views
18

Tôi có một ứng dụng được viết bằng C# cần có khả năng cấu hình bộ điều hợp mạng trong Windows. Tôi có điều này về cơ bản làm việc thông qua WMI, nhưng có một vài điều tôi không thích về giải pháp đó: đôi khi các thiết lập dường như không dính, và khi cáp mạng không được cắm vào, các lỗi được trả về từ WMI phương pháp, vì vậy tôi không thể nói nếu họ thực sự thành công hay không.Cách tốt nhất để lập trình cấu hình bộ điều hợp mạng trong .NET

Tôi cần có thể định cấu hình tất cả các cài đặt có sẵn thông qua kết nối mạng - Thuộc tính - Màn hình TCP/IP.

Cách tốt nhất để làm điều này là gì?

Trả lời

21

Bạn có thể sử dụng Process để tắt các lệnh netsh để đặt tất cả các thuộc tính trong hộp thoại mạng.

ví dụ: Để thiết lập một ipaddress tĩnh trên một adapter

netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1 

Để thiết lập nó để dhcp bạn muốn sử dụng

netsh interface ip set address "Local Area Connection" dhcp 

Để làm điều đó từ C# sẽ là

Process p = new Process(); 
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1"); 
p.StartInfo = psi; 
p.Start(); 

Cài đặt thành tĩnh có thể mất một vài giây để hoàn thành vì vậy nếu bạn cần, hãy đảm bảo bạn đợi cho quá trình thoát.

+0

Bất kỳ ý tưởng làm thế nào để nâng cao lệnh này để chạy với quyền quản trị viên? – Fuser97381

+0

Hãy xem http://stackoverflow.com/questions/133379/elevating-process-privilege-programatically – PaulB

+0

Cảm ơn, bạn là người giỏi nhất. – Fuser97381

2

Tôi có thể cho bạn biết cách trojans làm điều đó, sau khi phải làm sạch sau khi một vài trong số họ, là để thiết lập các khóa registry dưới HKEY_LOCAL_MACHINE. Những người chính mà họ đặt là những người DNS và cách tiếp cận chắc chắn gậy mà có thể được atested bởi bất cứ ai đã từng bị nhiễm và không còn có thể nhận được để windowsupdate.com, mcafee.com vv.

+0

"HKEY_LOCAL_MACHINE" không chính xác thu hẹp nơi để tìm chìa khóa quan trọng . Chi tiết hơn một chút sẽ làm cho câu trả lời này thực sự hữu ích. – mickeyf

0

với sự giúp đỡ của những câu trả lời @ PaulB của giúp

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 
Process p = new Process(); 
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address " + nics[0].Name + " static 192.168." + provider_ip + "." + index + " 255.255.255.0 192.168." + provider_ip + ".1 1"); 
p.StartInfo = psi; 
p.StartInfo.Verb = "runas"; 
p.Start(); 
1

Với mã SetIpAddress tôi và SetDHCP

/// <summary> 
    /// Sets the ip address. 
    /// </summary> 
    /// <param name="nicName">Name of the nic.</param> 
    /// <param name="ipAddress">The ip address.</param> 
    /// <param name="subnetMask">The subnet mask.</param> 
    /// <param name="gateway">The gateway.</param> 
    /// <param name="dns1">The DNS1.</param> 
    /// <param name="dns2">The DNS2.</param> 
    /// <returns></returns> 
    public static bool SetIpAddress(
     string nicName, 
     string ipAddress, 
     string subnetMask, 
     string gateway = null, 
     string dns1 = null, 
     string dns2 = null) 
    { 
     ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
     ManagementObjectCollection moc = mc.GetInstances(); 

     NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 
     NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName); 
     string nicDesc = nicName; 

     if (networkInterface != null) 
     { 
      nicDesc = networkInterface.Description; 
     } 

     foreach (ManagementObject mo in moc) 
     { 
      if ((bool)mo["IPEnabled"] == true 
       && mo["Description"].Equals(nicDesc) == true) 
      { 
       try 
       { 
        ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); 

        newIP["IPAddress"] = new string[] { ipAddress }; 
        newIP["SubnetMask"] = new string[] { subnetMask }; 

        ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); 

        if (gateway != null) 
        { 
         ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways"); 

         newGateway["DefaultIPGateway"] = new string[] { gateway }; 
         newGateway["GatewayCostMetric"] = new int[] { 1 }; 

         ManagementBaseObject setGateway = mo.InvokeMethod("SetGateways", newGateway, null); 
        } 


        if (dns1 != null || dns2 != null) 
        { 
         ManagementBaseObject newDns = mo.GetMethodParameters("SetDNSServerSearchOrder"); 
         var dns = new List<string>(); 

         if (dns1 != null) 
         { 
          dns.Add(dns1); 
         } 

         if (dns2 != null) 
         { 
          dns.Add(dns2); 
         } 

         newDns["DNSServerSearchOrder"] = dns.ToArray(); 

         ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDns, null); 
        } 
       } 
       catch 
       { 
        return false; 
       } 
      } 
     } 

     return true; 
    } 

    /// <summary> 
    /// Sets the DHCP. 
    /// </summary> 
    /// <param name="nicName">Name of the nic.</param> 
    public static bool SetDHCP(string nicName) 
    { 
     ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
     ManagementObjectCollection moc = mc.GetInstances(); 

     NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 
     NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName); 
     string nicDesc = nicName; 

     if (networkInterface != null) 
     { 
      nicDesc = networkInterface.Description; 
     } 

     foreach (ManagementObject mo in moc) 
     { 
      if ((bool)mo["IPEnabled"] == true 
       && mo["Description"].Equals(nicDesc) == true) 
      { 
       try 
       { 
        ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); 

        newDNS["DNSServerSearchOrder"] = null; 
        ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null); 
        ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); 
       } 
       catch 
       { 
        return false; 
       } 
      } 
     } 

     return true; 
    } 
Các vấn đề liên quan