目录
ScrollViewer:滚动视图
strokedasharray属性:用于创建虚线
NavigationUIVisibility="Hidden" :隐藏菜单方向键
TextBlock.LineHeight:改变行距
TextWrapping="Wrap":表示当文本长度超过容器长度时可以自动换行
这里只放了最核心的Page后台类文件的代码。注意命名空间的引用。
例1-1 DNS域名解析和IP地址转换的基本用法
public partial class DnsExamplePage : Page
{
public DnsExamplePage()
{
InitializeComponent();
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("获取www.cctv.com的所有IP地址:");
try
{
IPAddress[] ips = Dns.GetHostAddresses("www.cctv.com");
foreach (IPAddress ip in ips)
{
sb.AppendLine(ip.ToString());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "获取失败");
}
string hostName = Dns.GetHostName();
sb.AppendLine("获取本机所有IP地址:");
IPHostEntry me = Dns.GetHostEntry(hostName);
foreach(IPAddress ip in me.AddressList)
{
if(ip.AddressFamily == AddressFamily.InterNetwork)
{
sb.AppendLine("IPv4:" + ip.ToString());
}
else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
sb.AppendLine("IPv6:" + ip.ToString());
}
else
{
sb.AppendLine("其他:" + ip.ToString());
}
}
//IPv6回路测试地址
IPAddress localip = IPAddress.Parse("::1");
Output(sb, localip);
//IPv4回路测试地址
IPAddress localip1 = IPAddress.Parse("127.0.0.1");
Output(sb, localip1);
textBlock1.Text = sb.ToString();
}
private static void Output(StringBuilder sb,IPAddress localip)
{
IPEndPoint iep = new IPEndPoint(localip, 80);
if (localip.AddressFamily == AddressFamily.InterNetworkV6)
{
sb.Append("IPv6端点: " + iep.ToString());
}
else
{
sb.Append("IPv4端点: " + iep.ToString());
}
sb.Append(",端口 " + iep.Port);
sb.Append(",地址 " + iep.Address);
sb.AppendLine(",地址族 " + iep.AddressFamily);
}
}
例1-2 获取网络适配器信息
/// <summary>
/// NetworkInterfacePage.xaml 的交互逻辑
/// </summary>
public partial class NetworkInterfacePage : Page
{
public NetworkInterfacePage()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
sb.AppendLine("适配器个数:" + adapters.Length);
int index = 0;
foreach (NetworkInterface adapter in adapters)
{
index++;
//显示网络适配器描述信息、名称、类型、速度、MAC地址
sb.AppendLine("-----------------第" + index + "个适配器信息-------------------");
sb.AppendLine("描述信息:" + adapter.Description);
sb.AppendLine("名称:" + adapter.Name);
sb.AppendLine("类型:" + adapter.NetworkInterfaceType);
sb.AppendLine("速度:" + adapter.Speed / 1000 / 1000 + "M");
byte[] macBytes = adapter.GetPhysicalAddress().GetAddressBytes();
sb.AppendLine("MAC地址:" + BitConverter.ToString(macBytes));
//获取IPInterfaceProperties实例
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
//获取并显示DNS服务器IP地址信息
IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
if (dnsServers.Count > 0)
{
foreach (IPAddress dns in dnsServers)
{
sb.AppendLine("DNS服务器IP地址:" + dns);
}
}
}
textBlock1.Text = sb.ToString();
}
}
例1-3 网络流量统计
/// <summary>
/// IPGlobalStaticsPage.xaml 的交互逻辑
/// </summary>
public partial class IPGlobalStaticsPage : Page
{
public IPGlobalStaticsPage()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();
sb.AppendLine("本机注册域名 : " + properties.DomainName);
sb.AppendLine("接收数据包数 : " + ipstat.ReceivedPackets);
sb.AppendLine("转发数据包数 : " + ipstat.ReceivedPacketsForwarded);
sb.AppendLine("传送数据包数 : " + ipstat.ReceivedPacketsDelivered);
sb.AppendLine("丢弃数据包数 : " + ipstat.ReceivedPacketsDiscarded);
textBlock1.Text = sb.ToString();
}
}
PS:
写代码和找bug的时间比1:5 害,就每天写bug呗...憨憨落泪