NEWS

[C#] Hướng dẫn cấu hình địa chỉ IPAddress, SubnetMask, DefaultGetway, DNS Winform

[C#] Hướng dẫn cấu hình địa chỉ IPAddress, SubnetMask, DefaultGetway, DNS Winform
Đăng bởi: Thảo Meo - Lượt xem: 7529 21:28:11, 22/08/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách cấu hình card mạng Ethenet bao gồm: IP Address, Subnet Mask, Default GetWay, Primary DNS trên winform.

[C#] Set ipaddress, subnet mask, default getway, DNS

Dưới đây là giao diện demo ứng dụng thay đổi cấu hình card mạng:

config_ipaddress_csharp

Video demo ứng dụng:

Ở giao diện dưới các bạn thấy, mình sẽ có 2 button tương ứng:

  1. Khi bấm nút Dynamic DHCP thì chỉnh cho card mạng tự động get IP
  2. Bấm static config để lưu thông tin cấu hình các thông số chúng ta vừa nhập ở 5 ô textbox vào bao gồm: Ip, subnet, default getway, DNS Server.

Để thực hiện cấu hình thay đổi các thông tin card mạng chúng ta sẽ sử dụng lệnh CMD MS-DOS:

Mình sẽ giới thiệu các lệnh cơ bản làm việc với Card Mạng:

1. Hiện thị thông tin các card mạng đang có trên máy tính các bạn sử dụng lệnh sau:

netsh interface ipv4 show interfaces

card_ethenet_csharp

2. Lệnh thay đổi thông tin IpAddress, Subnet, Default Getway

netsh interface ip set address name="Local Area Connection" static 123.123.123.123 255.255.255.0 123.123.123.1 1

"Local Area Connection" => tên adapter card mạng các bạn muốn cấu hình.

"123.123.123.123"  => IP Addres

"255.255.255.0" => subnet mask.

"123.123.123.1" => gateway.

3. Lệnh thay đổi cấu hình DNS Server

Primary DNS:

netsh interface ip set dns name="Local Area Connection" static 208.67.222.222

Second DNS:

netsh interface ip add dns name="Local Area Connection" 208.67.220.220 index=2

4. Bật DHCP

netsh interface ip set address name="Local Area Connection" dhcp

Áp dụng, các hàm trên chúng ta sẽ đưa chúng vào App Winform để thực hiện lệnh.

Full source code set IPAddress C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConfigIPAddress
{
    public partial class Form1 : Form
    {
        string WifiIP;
        string WifiDns;
        string WifiName;
        public Form1()
        {
            InitializeComponent();
            WifiInf(out WifiIP, out WifiDns, out WifiName);
           
        }

        private static void WifiInf(out string ip, out string dns, out string nic)  // To get current wifi config
        {
            ip = "";
            dns = "";
            nic = "";
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {

                    foreach (IPAddress dnsAdress in ni.GetIPProperties().DnsAddresses)
                    {
                        if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            dns = dnsAdress.ToString();
                        }
                    }


                    foreach (UnicastIPAddressInformation ips in ni.GetIPProperties().UnicastAddresses)
                    {
                        if (ips.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !ips.Address.ToString().StartsWith("169")) //to exclude automatic ips
                        {
                            ip = ips.Address.ToString();
                            nic = ni.Name;
                        }
                    }
                }
            }

        }


        private void SetIP(string arg)  
        {
            try
            {
              
                ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
                psi.UseShellExecute = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.Verb = "runas";
                psi.Arguments = arg;
                Process.Start(psi);              

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }


        }
        

        private void btn_setIP_Click(object sender, EventArgs e)
        {
            string IpAddress = txt_ipaddress.Text;
            string Subnet = txt_subnet.Text;
            string wifiDefaultGetway = txt_default_getway.Text;


            string PrimaryDNS = txt_pri_dns.Text;
            string SecondDNS = txt_second_dns.Text;

            SetIP("/c netsh interface ip set address "" + WifiName + "" static " + IpAddress + " " + Subnet + " " + wifiDefaultGetway + " & netsh interface ip set dns "" + WifiName + "" static " + PrimaryDNS + $" & netsh interface ip add dns {WifiName} {SecondDNS} index=2");
        }

        private void btn_dhcp_Click(object sender, EventArgs e)
        {
            SetIP("/c netsh interface ip set address "" + WifiName + "" dhcp & netsh interface ip set dns "" + WifiName + "" dhcp");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://laptrinhvb.net");
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn cấu hình địa chỉ IPAddress, SubnetMask, DefaultGetway, DNS Winform
Đăng bởi: Thảo Meo - Lượt xem: 7529 21:28:11, 22/08/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.