常见获取系统信息如下:
1 获取当前系统时间
2 获取系统目录
3 获取计算机名称
4 获取当前程序目录
5 获取系统版本号
6 获取当前系统启动时间
7 控件显示提示信息
8 获取环境变量和变量值
9 获取屏幕分辨率
10 获取当前IP地址
11 检测是否联网
12 获取特殊文件路径(比如桌面)
13 获取当前EXE文件路径
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace 获取系统信息
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bt_GetNowTime_Click(object sender, EventArgs e)
{
MessageBox.Show(DateTime.Now. ToString());
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.SystemDirectory.ToString());//获取系统目录
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.MachineName);//获取计算机名
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.CurrentDirectory);//获取当前软件运行目录
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.OSVersion.VersionString);//获取操作系统版本号
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show((Environment.TickCount / 1000).ToString()+"秒");//获取当前电脑运行时间
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.InitialDelay = 400;//设置显示之前的时间
toolTip1.ReshowDelay = 500;//设置提示窗口出现的时间
toolTip1.ShowAlways = true;//设置是否显示提示窗口
toolTip1.SetToolTip(this.bt_ShowMsg, "提示信息");
}
private void button6_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.Columns.Add("环境变量",150,HorizontalAlignment.Left);//添加表头
listView1.Columns.Add("变量值", 150, HorizontalAlignment.Left);
ListViewItem myitem;
foreach(DictionaryEntry mEntry in Environment.GetEnvironmentVariables())
{
myitem = new ListViewItem(mEntry.Key.ToString(), 0);//创建一个item对象,环境变量
myitem.SubItems.Add(mEntry.Value.ToString());//添加子项集合,环境变量值
listView1.Items.Add(myitem);
}
}
private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show("分辨率:" + SystemInformation.VirtualScreen.Width.ToString() + " " + SystemInformation.VirtualScreen.Height.ToString());//获取屏幕分辨率
}
private void button8_Click(object sender, EventArgs e)
{
string s = "";
System.Net.IPAddress[] IpList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;//获取当前IP
foreach(var i in IpList)
{
s += i + "\n";
}
MessageBox.Show("IP:" + s);
}
private void button9_Click(object sender, EventArgs e)
{
if (SystemInformation.Network)
MessageBox.Show("已经联网");
else
MessageBox.Show("未联网");
}
private void button10_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));//获取特殊文件夹路径
}
private void button11_Click(object sender, EventArgs e)
{
MessageBox.Show(Application.ExecutablePath);//获取当前EXE文件路径
}
}
}