返回首页
当前位置: 主页 > 编程语言 > C#教程 >

C#罗列出磁盘信息_LIstview控件的使用教程

时间:2016-06-03 12:25来源:电脑教程学习网 www.etwiki.cn 编辑:admin

//listview1重命名为 lvInfo

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;

namespace 罗列出磁盘信息_LIstview控件的使用_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
lvInfo.View = View.Details;
ColumnHeader ch1 = new ColumnHeader();
ch1.Text = "Disk Name";
ch1.Width = 85;
lvInfo.Columns.Add(ch1);
ColumnHeader ch2 = new ColumnHeader();
ch2.Text = "Type";
ch2.Width = 85;
lvInfo.Columns.Add(ch2);
ColumnHeader ch3 = new ColumnHeader();
ch3.Text = "All Size";
ch3.Width = 85;
lvInfo.Columns.Add(ch3);
ColumnHeader ch4 = new ColumnHeader();
ch4.Text = "Free Size";
ch4.Width = 85;
lvInfo.Columns.Add(ch4);
string[] drive = Environment.GetLogicalDrives();
for (int i = 0; i < drive.Length; i++)
{
//实例化一个listview对象的子项
ListViewItem lvi1 = new ListViewItem();
lvi1.Text = drive[i];//第一列数据
lvi1.SubItems.Add(i.ToString());//第二列
lvi1.SubItems.Add(GetHardDiskTotalSize(i).ToString()+" G");//第三列
lvi1.SubItems.Add(GetHardDiskFreeSize(i).ToString()+" G");//第四列
lvInfo.Items.Add(lvi1);//添加列
}
}
/// <summary>
/// 获取磁盘总空间
/// </summary>
/// <param name="i">获取磁盘需要的下标 0 c盘 1 d盘</param>
/// <returns>磁盘总空间 long类型</returns>
public static long GetHardDiskTotalSize(int i)
{
long totalSize = new long();
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
totalSize = drives[i].TotalSize / (1024L * 1024 * 1024);
return totalSize;
}
public static long GetHardDiskFreeSize(int i)
{
long freeSize = new long();
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
freeSize = drives[i].AvailableFreeSpace / (1024 * 1024 * 1024);
return freeSize;
}
}
}

C#罗列出磁盘信息_LIstview控件的使用教程
C#罗列出磁盘信息_LIstview控件的使用教程

 

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):c# c#技巧 c#实例教程 c#源代码 c#基础教程
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐内容