asp.net获取磁盘相关信息
使用System.IO名称空间下的DriveInfo类别,可以获取磁盘相关信息。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private DataTable GetDriveInfo(string driveName)
{
//new一个实例表
DataTable dataTable = new DataTable();
DataColumn dataColumn;
//new一个字段
dataColumn = new DataColumn();
//字段存储的数据类型
dataColumn.DataType = Type.GetType("System.String");
//Gets the name of a drive.
dataColumn.ColumnName = "Name";
//把这个字段添加至表中。
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Indicates the amount of available free space on a drive.
dataColumn.ColumnName = "AvailableFreeSpace";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the name of the file system, such as NTFS or FAT32.
dataColumn.ColumnName = "Format";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the drive type.
dataColumn.ColumnName = "Type";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets a value indicating whether a drive is ready.
dataColumn.ColumnName = "Ready";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the root directory of a drive.
dataColumn.ColumnName = "RootDirectory";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the total amount of free space available on a drive.
dataColumn.ColumnName = "FreeSpace";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the total size of storage space on a drive.
dataColumn.ColumnName = "TotalSize";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets or sets the volume label of a drive.
dataColumn.ColumnName = "Volume";
dataTable.Columns.Add(dataColumn);
//new一个DriveInfo对象,并传入盘符
DriveInfo objDi = new DriveInfo(driveName);
DataRow dataRow;
dataRow = dataTable.NewRow(); //表创建一行
//下面是为字段赋值
dataRow["Name"] = objDi.Name.ToString();
dataRow["AvailableFreeSpace"] = objDi.AvailableFreeSpace.ToString();
dataRow["Format"] = objDi.DriveFormat.ToString();
dataRow["Type"] = objDi.DriveType.ToString();
dataRow["Ready"] = objDi.IsReady.ToString();
dataRow["RootDirectory"] = objDi.RootDirectory.ToString();
dataRow["FreeSpace"] = objDi.TotalFreeSpace.ToString();
dataRow["TotalSize"] = objDi.TotalSize.ToString();
dataRow["Volume"] = objDi.VolumeLabel.ToString();
//把行添加至表
dataTable.Rows.Add(dataRow);
dataTable.AcceptChanges();
return dataTable;
}
{
//new一个实例表
DataTable dataTable = new DataTable();
DataColumn dataColumn;
//new一个字段
dataColumn = new DataColumn();
//字段存储的数据类型
dataColumn.DataType = Type.GetType("System.String");
//Gets the name of a drive.
dataColumn.ColumnName = "Name";
//把这个字段添加至表中。
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Indicates the amount of available free space on a drive.
dataColumn.ColumnName = "AvailableFreeSpace";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the name of the file system, such as NTFS or FAT32.
dataColumn.ColumnName = "Format";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the drive type.
dataColumn.ColumnName = "Type";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets a value indicating whether a drive is ready.
dataColumn.ColumnName = "Ready";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the root directory of a drive.
dataColumn.ColumnName = "RootDirectory";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the total amount of free space available on a drive.
dataColumn.ColumnName = "FreeSpace";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets the total size of storage space on a drive.
dataColumn.ColumnName = "TotalSize";
dataTable.Columns.Add(dataColumn);
dataColumn = new DataColumn();
dataColumn.DataType = Type.GetType("System.String");
//Gets or sets the volume label of a drive.
dataColumn.ColumnName = "Volume";
dataTable.Columns.Add(dataColumn);
//new一个DriveInfo对象,并传入盘符
DriveInfo objDi = new DriveInfo(driveName);
DataRow dataRow;
dataRow = dataTable.NewRow(); //表创建一行
//下面是为字段赋值
dataRow["Name"] = objDi.Name.ToString();
dataRow["AvailableFreeSpace"] = objDi.AvailableFreeSpace.ToString();
dataRow["Format"] = objDi.DriveFormat.ToString();
dataRow["Type"] = objDi.DriveType.ToString();
dataRow["Ready"] = objDi.IsReady.ToString();
dataRow["RootDirectory"] = objDi.RootDirectory.ToString();
dataRow["FreeSpace"] = objDi.TotalFreeSpace.ToString();
dataRow["TotalSize"] = objDi.TotalSize.ToString();
dataRow["Volume"] = objDi.VolumeLabel.ToString();
//把行添加至表
dataTable.Rows.Add(dataRow);
dataTable.AcceptChanges();
return dataTable;
}
指定显示C盘信息至GridView控件上:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
//传入C盘
this.GridView1.DataSource = GetDriveInfo("c");
this.GridView1.DataBind();
}
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
//传入C盘
this.GridView1.DataSource = GetDriveInfo("c");
this.GridView1.DataBind();
}
或者您可以显示所有磁盘,显示RadioButtonList控件上:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<asp:RadioButtonList ID="rblDriveList" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" RepeatColumns="10" OnSelectedIndexChanged="rblDriveList_SelectedIndexChanged">
</asp:RadioButtonList>
</asp:RadioButtonList>
给RadioButtonList控件绑定数据源:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
List<string> driveList = new List<string>();
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo di in allDrives)
{
driveList.Add(di.ToString().Substring(0,1));
}
this.rblDriveList.DataSource = driveList;
this.rblDriveList.DataBind();
}
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
List<string> driveList = new List<string>();
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo di in allDrives)
{
driveList.Add(di.ToString().Substring(0,1));
}
this.rblDriveList.DataSource = driveList;
this.rblDriveList.DataBind();
}
点击磁盘盘符显示相应盘符信息:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void rblDriveList_SelectedIndexChanged(object sender, EventArgs e)
{
if (rblDriveList.SelectedItem.Value == string.Empty) return;
this.GridView1.DataSource = GetDriveInfo(rblDriveList.SelectedItem.Value);
this.GridView1.DataBind();
}
{
if (rblDriveList.SelectedItem.Value == string.Empty) return;
this.GridView1.DataSource = GetDriveInfo(rblDriveList.SelectedItem.Value);
this.GridView1.DataBind();
}