C#获取常用系统信息

该代码示例展示了如何利用C#获取各种系统信息,包括当前时间、系统目录、计算机名称、程序目录、系统版本号、启动时间、环境变量、屏幕分辨率、IP地址、网络连接状态、特殊文件路径(如桌面)以及当前EXE文件路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常见获取系统信息如下:

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文件路径
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值