欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现获取运行平台系统信息的方法

程序员文章站 2024-02-20 15:11:40
本文实例讲述了c#获取运行平台系统信息的方法,主要可以实现c#获取系统启动经过的毫秒数,相连网络域名,系统启动经过的毫秒数等,并有关于listview控件的相关操作。 具...

本文实例讲述了c#获取运行平台系统信息的方法,主要可以实现c#获取系统启动经过的毫秒数,相连网络域名,系统启动经过的毫秒数等,并有关于listview控件的相关操作。

具体的实现代码如下:

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace 获取系统环境和平台信息
{
 public class form1 : system.windows.forms.form
 {
 private system.windows.forms.button button1;
 private system.windows.forms.button button2;
 private system.windows.forms.listview listview1;
 private system.windows.forms.columnheader columnheader1;
 private system.windows.forms.columnheader columnheader2;
 private system.componentmodel.container components = null;
 public form1()
 {
  initializecomponent();
 }
 protected override void dispose( bool disposing )
 {
  if( disposing )
  {
  if (components != null)
  {
   components.dispose();
  }
  }
  base.dispose( disposing );
 }
 #region windows 窗体设计器生成的代码
 private void initializecomponent()
 {
  this.button1 = new system.windows.forms.button();
  this.button2 = new system.windows.forms.button();
  this.listview1 = new system.windows.forms.listview();
  this.columnheader1 = new system.windows.forms.columnheader();
  this.columnheader2 = new system.windows.forms.columnheader();
  this.suspendlayout();
  // button1
  this.button1.location = new system.drawing.point(48, 224);
  this.button1.name = "button1";
  this.button1.size = new system.drawing.size(56, 32);
  this.button1.tabindex = 4;
  this.button1.text = "获取";
  this.button1.click += new system.eventhandler(this.button1_click);
  // button2
  this.button2.location = new system.drawing.point(184, 224);
  this.button2.name = "button2";
  this.button2.size = new system.drawing.size(56, 32);
  this.button2.tabindex = 5;
  this.button2.text = "退出";
  this.button2.click += new system.eventhandler(this.button2_click);
  //
  // listview1
  this.listview1.columns.addrange(new system.windows.forms.columnheader[] {
           this.columnheader1,
           this.columnheader2});
  this.listview1.gridlines = true;
  this.listview1.location = new system.drawing.point(16, 24);
  this.listview1.name = "listview1";
  this.listview1.size = new system.drawing.size(256, 184);
  this.listview1.tabindex = 6;
  this.listview1.view = system.windows.forms.view.details;
  // columnheader1
  this.columnheader1.text = "属性";
  this.columnheader1.width = 100;
  // columnheader2
  this.columnheader2.text = "值";
  this.columnheader2.width = 175;
  // form1
  this.autoscalebasesize = new system.drawing.size(6, 14);
  this.clientsize = new system.drawing.size(292, 273);
  this.controls.add(this.listview1);
  this.controls.add(this.button2);
  this.controls.add(this.button1);
  this.name = "form1";
  this.text = "获取系统环境和平台信息";
  this.resumelayout(false);
 }
 #endregion
 [stathread]
 static void main()
 {
  application.run(new form1());
 }
 private void button2_click(object sender, system.eventargs e)
 {
  // 关闭当前窗体
  this.close();
 }
 private void button1_click(object sender, system.eventargs e)
 {
  listview1.items.clear(); // 清除listview控件中的项
  listviewitem listviewitem;
  try
  {
  // 加入计算机名
  listviewitem = new listviewitem("计算机名", 0);
  listviewitem.subitems.add(environment.machinename);
  listview1.items.add(listviewitem);
  // 加入当前平台名
  listviewitem = new listviewitem("当前平台名", 0);
  listviewitem.subitems.add(environment.osversion.platform.tostring());
  listview1.items.add(listviewitem);
  // 加入平台版本号
  listviewitem = new listviewitem("平台版本号", 0);
  listviewitem.subitems.add(environment.osversion.version.tostring());
  listview1.items.add(listviewitem);
  // 与系统相连的网络名
  listviewitem = new listviewitem("相连网络域名", 0);
  listviewitem.subitems.add(environment.userdomainname);
  listview1.items.add(listviewitem);
  // 系统目录路径
  listviewitem = new listviewitem("系统启动经过的毫秒数", 0);
  listviewitem.subitems.add(environment.systemdirectory );
  listview1.items.add(listviewitem);
  // 系统当前时间
  listviewitem = new listviewitem("系统当前时间", 0);
  listviewitem.subitems.add(datetime.now.tostring());
  listview1.items.add(listviewitem);
  // 系统启动后经过的毫秒数
  listviewitem = new listviewitem("系统启动经过的毫秒数", 0);
  listviewitem.subitems.add(environment.tickcount.tostring());
  listview1.items.add(listviewitem);
  }
  catch(exception exc)
  {
  messagebox.show(exc.message, "提示");
  }
 }
 // 为避免编写的代码冗长,添加 additem 方法
 public void additem(string sitem)
 {
  // 添加项 sitem 到 listview1 中
  listview1.items.add(sitem);
 }
 }
}