有时,必须收集有关运行应用程序的计算机的信息,以便在代码中做出决策。 例如,你可能有一个仅在连接到特定网络域时才适用的函数;在这种情况下,需要一种方法来确定域,并在域不存在时禁用该函数。
Windows 窗体应用程序可以使用 SystemInformation 该类来确定有关计算机在运行时的一些事项。 以下示例演示如何使用 SystemInformation 类检索 UserName 和 UserDomainName:
Dim User As String = Windows.Forms.SystemInformation.UserName
Dim Domain As String = Windows.Forms.SystemInformation.UserDomainName
MessageBox.Show("Good morning " & User & ". You are connected to " _
& Domain)
string User = SystemInformation.UserName;
string Domain = SystemInformation.UserDomainName;
MessageBox.Show("Good morning " + User + ". You are connected to "
+ Domain);
该类的所有成员都是只读的 SystemInformation ;不能修改用户的设置。 该类有 100 多个成员,提供关于从连接到计算机的显示器数量(MonitorCount)到 Windows 资源管理器中图标间距(IconHorizontalSpacing 和 IconVerticalSpacing)的所有信息。
类SystemInformation的一些更有用的成员包括ComputerName、DbcsEnabled、PowerStatus和TerminalServerSession。