最新版本号[免费下载]

ASP.NET 磁盘操作 文件体系 IO体系 DriveInfo类-即aspx调用Driveinfo方法

作者: 发布时间:2020-05-30 来源:本站原创 点击数:

DriveInfo可以输出硬盘信息,但如何在aspx的webform输出呢?

说明:运行环境为VS2010

以下是前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DriverInfoDemo_my.aspx.cs" Inherits="day19_DriverInfoDemo_my" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="DriverName" HeaderText="分区名称" />
        <asp:BoundField DataField="DriveType" HeaderText="分区类型" />
        <asp:BoundField DataField="DriveFormat" HeaderText="文件系统" />
        <asp:BoundField DataField="VolumeLabel" HeaderText="卷标" />
        <asp:BoundField DataField="TotalFreeSpace" HeaderText="空闲容量" />
        <asp:BoundField DataField="TotalSize" HeaderText="总容量" />
        <asp:BoundField DataField="Percent" DataFormatString="{0}%" HeaderText="使用百分比" />
        <asp:BoundField DataField="AvailableFreeSpace" HeaderText="磁盘配额" />
        </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

以下为后台源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class day19_DriverInfoDemo_my : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            InitDrivers();
        }
    }
    private void InitDrivers()
    {
        //获取当前机器上所有磁盘分区信息
        DriveInfo[] drivers = DriveInfo.GetDrives();
        DataTable data = new DataTable();
        data.Columns.Add("DriverName", typeof(string));
        data.Columns.Add("DriveType",typeof(string));
        data.Columns.Add("DriveFormat",typeof(string));
        data.Columns.Add("VolumeLabel",typeof(string));
        data.Columns.Add("TotalFreeSpace",typeof(long));
        data.Columns.Add("TotalSize",typeof(long));
        data.Columns.Add("Percent",typeof(float));
        data.Columns.Add("AvailableFreeSpace", typeof(float));
        foreach( DriveInfo info in drivers )
        {
            //这里仅列出硬盘分区的使用情况
            if (info.DriveType == DriveType.Fixed)
            {
                DataRow row = data.NewRow();
                //磁盘分区名称
                row["DriverName"] = info.Name;
                //磁盘分区类型,如软驱、硬盘或者光驱等
                row["DriveType"] = info.DriveType;
                //磁盘分区文件系统,如FAT16、FAT32及NTFS等
                row["DriveFormat"] = info.DriveFormat;
                //磁盘分区卷标
                row["VolumeLabel"] = info.VolumeLabel;
                //磁盘空闲容量
                row["TotalFreeSpace"] = info.TotalFreeSpace;
                //磁盘容量
                row["TotalSize"] = info.TotalSize;
                //磁盘使用量百分比
                row["Percent"] = info.TotalFreeSpace * 100 / info.TotalSize;
                //当前用户的磁盘配额
                row["AvailableFreeSpace"] = info.AvailableFreeSpace;
                data.Rows.Add(row);
            }
        }
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}
本文责任编辑: 加入会员收藏夹 点此参与评论>>
复制本网址-发给QQ/微信上的朋友
AI智能听书
选取音色