C#讀取本地網(wǎng)絡(luò)配置信息的方法小結(jié)
應(yīng)用場景
- 網(wǎng)絡(luò)診斷工具
開發(fā)網(wǎng)絡(luò)診斷工具時,需要獲取本地網(wǎng)絡(luò)接口的信息,如IP地址、子網(wǎng)掩碼、默認網(wǎng)關(guān)等,以幫助診斷網(wǎng)絡(luò)連接問題。 - 動態(tài)網(wǎng)絡(luò)配置
在某些應(yīng)用場景中,如云計算或容器化部署,可能需要根據(jù)當前環(huán)境動態(tài)配置網(wǎng)絡(luò)設(shè)置,例如自動配置IP地址或更新DNS服務(wù)器地址。 - 監(jiān)控和日志
網(wǎng)絡(luò)配置信息對于監(jiān)控網(wǎng)絡(luò)狀態(tài)和記錄網(wǎng)絡(luò)活動日志至關(guān)重要。通過程序獲取這些信息,可以幫助開發(fā)者或系統(tǒng)管理員更好地了解網(wǎng)絡(luò)行為和識別潛在問題。
示例
以下是使用C#讀取本地網(wǎng)絡(luò)配置信息的幾個示例。
示例1: 獲取所有網(wǎng)絡(luò)接口的信息
此示例展示了如何獲取本地計算機上所有網(wǎng)絡(luò)接口的基本信息。
using System; using System.Windows; using System.Net.NetworkInformation; class Program { static void Main() { StringBuilder sb = new StringBuilder(); sb.AppendLine($"本地網(wǎng)絡(luò)接口信息:");// 獲取并遍歷所有網(wǎng)絡(luò)接口 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { sb.AppendLine($"名稱: {ni.Name}"); sb.AppendLine($"描述: {ni.Description}"); sb.AppendLine($"狀態(tài): {ni.OperationalStatus}"); sb.AppendLine($"MAC 地址: {ni.GetPhysicalAddress()}"); sb.AppendLine("======================================="); } MessageBox.Show(sb.ToString()); } }
示例2: 獲取特定網(wǎng)絡(luò)接口的IP配置信息
此示例展示了如何獲取指定網(wǎng)絡(luò)接口的IP地址、子網(wǎng)掩碼和默認網(wǎng)關(guān)。
using System; using System.Linq; using System.Windows; using System.Net.NetworkInformation; using System.Net.Sockets; class Program { static void Main() { // 指定要檢索的網(wǎng)絡(luò)接口名稱 string interfaceName = "Wi-Fi"; var networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(ni => ni.Name == interfaceName); string message = ""; if (networkInterface != null) { message += $"網(wǎng)絡(luò)接口: {networkInterface.Name}\n"; var ipProperties = networkInterface.GetIPProperties(); // 獲取IPv4配置信息 var ipv4Properties = ipProperties.UnicastAddresses.FirstOrDefault(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork); if (ipv4Properties != null) { message += $"IP 地址: {ipv4Properties.Address}\n"; message += $"子網(wǎng)掩碼: {ipv4Properties.IPv4Mask}\n"; } // 獲取默認網(wǎng)關(guān) var gatewayAddress = ipProperties.GatewayAddresses.FirstOrDefault(ga => ga.Address.AddressFamily == AddressFamily.InterNetwork); if (gatewayAddress != null) { message += $"默認網(wǎng)關(guān): {gatewayAddress.Address}\n"; } } else { message = "指定的網(wǎng)絡(luò)接口未找到。"; } MessageBox.Show(message); } }
示例3: 獲取DNS服務(wù)器地址
此示例展示了如何獲取和顯示本地網(wǎng)絡(luò)接口配置的DNS服務(wù)器地址。
using System; using System.Net.NetworkInformation; using System.Linq; using System.Windows; class Program { static void Main() { // 選擇一個活動的網(wǎng)絡(luò)接口 var activeInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(ni => ni.OperationalStatus == OperationalStatus.Up); string message = ""; if (activeInterface != null) { message += $"網(wǎng)絡(luò)接口: {activeInterface.Name}\n"; var ipProperties = activeInterface.GetIPProperties(); // 獲取DNS服務(wù)器地址 var dnsAddresses = ipProperties.DnsAddresses; foreach (var dns in dnsAddresses) { message += $"DNS服務(wù)器地址: {dns}\n"; } } else { message = "未找到活動的網(wǎng)絡(luò)接口。"; } MessageBox.Show(message, "網(wǎng)絡(luò)信息", MessageBoxButton.OK, MessageBoxImage.Information); } }
以上示例展示了如何在C#中讀取本地網(wǎng)絡(luò)配置信息,包括網(wǎng)絡(luò)接口的基本信息、IP配置以及DNS服務(wù)器地址。通過這些信息,開發(fā)者可以開發(fā)出功能豐富的網(wǎng)絡(luò)應(yīng)用程序,滿足不同的業(yè)務(wù)需求。
到此這篇關(guān)于C#讀取本地網(wǎng)絡(luò)配置信息的方法小結(jié)的文章就介紹到這了,更多相關(guān)C#讀取網(wǎng)絡(luò)配置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何通過C#/VB.NET調(diào)整PDF文檔頁邊距
PDF邊距是頁面主要內(nèi)容區(qū)域和頁面邊緣之間的距離。與Word頁邊距不同,PDF文檔的頁邊距很難更改。本文將介紹如何在不更改頁面大小的情況下使用C#/VB.NET?代碼調(diào)整PDF文檔的頁邊距,需要的可以參考一下2023-04-04C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列示例詳解
這篇文章主要給大家介紹了關(guān)于C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09C#接口INotifyPropertyChanged使用方法
這篇文章介紹了C#接口INotifyPropertyChanged的使用方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01C#實現(xiàn)幾十萬級數(shù)據(jù)導出Excel及Excel各種操作實例
本篇文章主要介紹了C#實現(xiàn)幾十萬級數(shù)據(jù)導出Excel及Excel各種操作實例,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-02-02