Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
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:

C#
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.
Posted
Updated 14-Apr-16 21:00pm
v2
Comments
Dave Kreskowiak 13-Apr-16 22:23pm    
That doesn't make sense because ASP.NET code runs entirely on the web server and won't have any mapped drives at all.

If you're talking about the mapped drives on the client (browser) you're still out of luck because code running in the web browser (javascript) doesn't have access to the client machines resources, like drives.
Member 8010354 13-Apr-16 23:48pm    
Actually, that drives are mapped to My Computer in my laptop. So from there i have to read the drive name, total size and free space.
So it's not possible?
Dave Kreskowiak 14-Apr-16 0:17am    
You're going to have to fill in the details. This still isn't enough information.

So does every other PC in the network have drive letters mapped to the shares exposed by your laptop? Is the IIS server hosting this ASP.NET application running on your laptop?
Member 8010354 14-Apr-16 3:39am    
Actually it's remote machine. So first i want get the MY COMPUTER drive details in my laptop so later i can figure out how to get the same for mapped drives.
Sergey Alexandrovich Kryukov 14-Apr-16 1:24am    
I'm afraid to ask: aren't you "required" to get information on client-side volumes? :-)
I'm just curious, because, no matter what disk space is "required" to be read, case closed.
—SA

 
Share this answer
 
Comments
Member 8010354 14-Apr-16 3:31am    
Hi, Thank you providing it. I tried this code but when i execute, i see blank screen in output. Can you let me know where did i missed like i'm not getting any error as well.
Member 8010354 14-Apr-16 3:50am    
Below is my program:

public partial class DriveThreshold : System.Web.UI.Page
{
class Report
{
public static void Main()
{
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);
}
}
}
}

In ASPX page:

<div>
<asp:Button ID="DriveInfor" runat="server" Text="Get the Drive Info" OnClick="DriveInfo_Click" />
</div>
Richard MacCutchan 14-Apr-16 3:57am    
As explained earlier you are getting information on the drives connected to the server. You cannot do this from a web browser.
Member 8010354 14-Apr-16 4:34am    
@Richard: Can you please suggest me which way to go.
Richard MacCutchan 14-Apr-16 5:08am    
Which way to go for what? We have explained that you cannot do this in a browser. So your only option is to create a desktop application that produces the information.
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);
}
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900