C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Resources » Articles » .NET Framework »

How to find free disk space and total space of a disk drive


Posted Date: 23 Jun 2004    Resource Type: Articles    Category: .NET Framework
Author: Tony JohnMember Level: Diamond    
Rating: Points: 10



There is no class available in .NET which allow us to access the disk properties directly. However, there are couple of ways to find the free space and other disk drive properties.

One option is to use InteropServices to call Windows APIs directly. InteropServices allow us to call un managed code and this can be used to call the Windows API "GetDiskFreeSpaceEx". This API returns couple of disk properties including available free space, total space and total free space.

The following sample code demonstrates how to call the Windows API GetDiskFreeSpaceEx to retrieve these values.

Sample Code

Step 1: Import the namespace "System.Runtime.InteropServices" :

using System.Runtime.InteropServices;


Step 2: Declare the Win32 API using the attribute "DllImport" so that it can be called from managed code.

[DllImport("kernel32", CharSet=CharSet.Auto)]
static extern int GetDiskFreeSpaceEx(
string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);


Step 3: Call the API from managed code (C# or VB.NET) to retrieve information about the specific disk drive.

ulong freeBytesAvailable, totalBytes, freeBytes;

// Call the Win32 API to retrieve disk space information.
GetDiskFreeSpaceEx ( "C:", out freeBytesAvailable, out totalBytes, out freeBytes );

System.Diagnostics.Debug.WriteLine( "Total space : " + totalBytes + " Bytes" );
System.Diagnostics.Debug.WriteLine( "Free space : " + freeBytes + " Bytes" );
System.Diagnostics.Debug.WriteLine( "Available Free space : " + freeBytesAvailable + " Bytes" );


Summary

The above sample shows how the InteropServices can be used to call Windows APIs from C# and how to retrieve the disk space information.

A better .NET way of doing this would be using the class "ManagementObjectSearcher" which is part of the "Windows Management Instrumentation (WMI)" classes provided by .NET Framework. The "System.Management" namespace in .NET provides many useful classes that can be used to access Operating System properties including disk drive properties.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to find difference between two Dates in C# or VB.NET ?
Previous Resource: Creating MDI application using C# (Walkthrough)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use