如何读取和显示磁盘空间 [英] How to read and display the disk space

查看:75
本文介绍了如何读取和显示磁盘空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个要求,我需要在C#(Asp)中显示所有映射驱动器的磁盘空间(总大小和可用空间) 。净)。我是ASP.NET的新手。



我通过下面的代码实现了它:



  protected   void  DriveInfo_Click(对象发​​件人,EventArgs e)
{
DriveInfo [] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
Console.WriteLine( Drive {0},d.Name);
Console.WriteLine( 驱动器类型:{0},d.DriveType);
if (d.IsReady == true
{
Console.WriteLine( 卷标:{0},d.VolumeLabel);
Console.WriteLine( 文件系统:{0},d.DriveFormat);
Console.WriteLine( 当前用户的可用空间:{0,15}字节 ,d.AvailableFreeSpace);

Console.WriteLine( 总可用空间:{0,15}字节,d.TotalFreeSpace);

Console.WriteLine( 驱动器总大小:{0,15}字节,d.TotalSize);
}
}
}





当我使用断点时,我得到了所需的信息,但是如何在gridview中显示数据?



任何人都可以帮助我实现它?我该怎么办?



我尝试过:



我正在尝试浏览,但由于我是asp.net的新手,我无法掌握任何特定的网站,因为术语对我来说有点混乱。

解决方案

< blockquote>请参阅 DriveInfo.AvailableFreeSpace Property(System.IO) ) [ ^ ]。


映射的驱动器不是实际的驱动器。它们是网络中机器中的文件夹/目录。

您将其路径保存为驱动器,因此您将看到的可用空间或已用空间是该文件夹驱动器的空间。



所需的c#代码将是



使用System.IO; 

foreach(驱动器中的var项)
{
Console.WriteLine(驱动器:{0} TotalSpace:{1} FreeSpace:{2},item.Name, item.TotalSize,item.AvailableFreeSpace);
}


使用Response.write代替Console.WriteLine在网页上显示它。

或add标签到您的页面并使用值设置标签文本。


Hi all,

I have a requirement where i need to display the disk space (total size and free space left) for all mapped drives in C# (Asp.net). I'm new to ASP.NET.

I achieved it by the code mentioned below:

protected void DriveInfo_Click(object sender, EventArgs e)
   {
       DriveInfo[] allDrives = DriveInfo.GetDrives();

       foreach (DriveInfo d in allDrives)
       {
           Console.WriteLine("Drive {0}", d.Name);
           Console.WriteLine("  Drive type: {0}", d.DriveType);
           if (d.IsReady == true)
           {
               Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
               Console.WriteLine("  File system: {0}", d.DriveFormat);
               Console.WriteLine("  Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);

               Console.WriteLine("  Total available space:          {0, 15} bytes", d.TotalFreeSpace);

               Console.WriteLine("  Total size of drive:            {0, 15} bytes ", d.TotalSize);
           }
       }
   }



When i use breakpoint, i got the required information but how to display the data in gridview?

Anyone can help me achieving it? How can i do it?

What I have tried:

I'm trying browsing but as i'm new to asp.net, i'm unable to hold of any particular site casue the terminology is little confusing for me.

解决方案

See DriveInfo.AvailableFreeSpace Property (System.IO)[^].


Mapped drives are not actual drives. they are folder/directories in a machine in your network.
you are saving their path as drive, so the free space or used space you will see is the space of that folder's drive.

Required c# code will be

using System.IO;

foreach (var item in drives)
{
Console.WriteLine("Drive: {0} TotalSpace:{1} FreeSpace:{2}",item.Name,item.TotalSize ,item.AvailableFreeSpace);
}


Instead of Console.WriteLine, Use Response.write to display it on web page.
or add labels to your page and set text of label with the values.


这篇关于如何读取和显示磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆