i m working on installer project . where i need to extract files to the disk. how can i calculate the disk space available on hard disk using c#??
A:
Do you need to care? Just write the files to the disk and handle the errors as needed - it's assumed that you'll have to implement rollback anyways in case something non space-related happens, so just treat "no disk space" as just another error that you need to rollback for.
Paul Betts
2010-07-09 05:21:00
Rather sloppy approach, don't you think? Getting available drive space ahead of time is a rather trivial matter, and it can save the user a lot of hassle when installing a program.
jrista
2010-07-09 05:23:39
I agree you should warn that the write will be impossibly but you'd still need to handle the drive running out of space during the copy.
Graphain
2010-07-09 05:28:18
I don't think it's sloppy at all, and it's far worse when a program estimates wrong - there's two answers on this thread and so far, neither of them are complete. Not to mention, what if you have to install some files on the system drive and some in the drive they asked for? How do you explain *that* to the user?
Paul Betts
2010-07-09 05:29:23
i think it would still be nice that a user is informed about the remaining free space rather than throwing an error during the installation process. Rollback consumes time, so why go for installation when you can prevent that before it happens?
hallie
2010-07-09 05:33:33
Wow, this answer is in need for a compensation upvote. We are running multitasking operating systems these days folks. +1.
Hans Passant
2010-07-10 23:43:43
+3
A:
Use the System.IO.DriveInfo class. There are two options available...one that takes disk quotas into account:
var drive = new DriveInfo("c");
long freeSpaceInBytes = drive.AvailableFreeSpace;
and one that simply provides the total free space:
var drive = new DriveInfo("c");
long freeSpaceInBytes = drive.TotalFreeSpace;
jrista
2010-07-09 05:22:02
+1
A:
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.totalfreespace.aspx
Copied from the link
using System;
using System.IO;
class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File 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);
}
}
}
}
Kai
2010-07-09 05:24:29