- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[C#] Hướng dẫn cấu hình địa chỉ IPAddress, SubnetMask, DefaultGetway, DNS Winform
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:
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:
- Khi bấm nút Dynamic DHCP thì chỉnh cho card mạng tự động get IP
- 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
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!