新建WPF程序OperatingSystemDemo。在默认的MainWindow.xaml进行设计。
一、MainWindow.xaml设计相关代码如下:
<Window x:Class="OperatingSystemDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OperatingSystemDemo"
mc:Ignorable="d"
Title="MainWindow" Height="850" Width="1000">
<RichTextBox x:Name="txtOperatingSystem" HorizontalAlignment="Left" Margin="0,15,0,0" VerticalAlignment="Top" Height="800" Width="1000"/>
</Window>
二、相对应的代码MainWindow.xaml.cs,源代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace OperatingSystemDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
/*
* System.Environment:静态类。
* 提供有关当前环境和平台的信息以及操作它们的方法。 无法继承此类。
*/
StringBuilder sb = new StringBuilder("显示本程序环境和平台的相关信息:\n");
sb.Append($"当前工作目录的完全限定路径:{Environment.CurrentDirectory}\n");
sb.Append($"CPU个数:{Environment.ProcessorCount}\n");
sb.Append($"命令行:{Environment.CommandLine}\n");
sb.Append($"退出代码:{Environment.ExitCode}\n");
sb.Append($"系统启动后经过的毫秒数:{Environment.TickCount}\n");
sb.Append($"操作系统的内存页的字节数:{Environment.SystemPageSize}\n");
string lineBytes = string.Join(",", Encoding.Default.GetBytes(Environment.NewLine));
sb.Append($"换行字符串【对应的字节】:{lineBytes}。即【回车换行 \"\\r\\n\"】\n");
sb.Append($"公共语言运行时版本:{Environment.Version}\n");
sb.Append($"映射到进程上下文的物理内存字节的数目:{Environment.WorkingSet / 1024}KB\n");
sb.Append($"操作系统信息:{Environment.OSVersion}.操作系统平台:【{Environment.OSVersion.Platform}】\n");
sb.Append($"当前进程是否为 64 位进程:【{Environment.Is64BitProcess}】\n");
sb.Append($"当前操作系统是否为 64 位操作系统:【{Environment.Is64BitOperatingSystem}】\n");
sb.Append($"已登录到Windows的人员的用户名:{Environment.UserName}\n");
sb.Append($"与当前用户关联的网络域名:{Environment.UserDomainName}\n");
sb.Append($"当前托管线程的唯一标识符:{Environment.CurrentManagedThreadId}\n");
sb.Append($"系统目录的完全限定路径:{Environment.SystemDirectory}\n");
sb.Append($"本地计算机的 NetBIOS 名称:{Environment.MachineName}\n");
sb.Append($"计算机硬盘逻辑驱动器:【{string.Join("☆", Environment.GetLogicalDrives())}】\n");
sb.Append($"当前进程的特殊目录信息:\n");
string[] specialFolderList = Enum.GetNames(typeof(Environment.SpecialFolder));
for (int i = 0; i < specialFolderList.Length; i++)
{
sb.Append($" 特殊目录名【{specialFolderList[i]}】:{Environment.GetFolderPath((Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), specialFolderList[i]))}\n");
}
sb.Append($"堆栈跟踪信息:{Environment.StackTrace}\n");
IDictionary keyValuePairs = Environment.GetEnvironmentVariables();
sb.Append($"当前进程的环境变量信息:\n");
foreach (string key in keyValuePairs.Keys)
{
sb.Append($" {key}:{keyValuePairs[key]}\n");
}
txtOperatingSystem.AppendText( sb.ToString());
}
}
}