using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string drive = @"D:\";
System.IO.DriveInfo di = new System.IO.DriveInfo(drive);
Console.WriteLine(di.TotalFreeSpace);
Console.WriteLine(di.VolumeLabel);
System.IO.DirectoryInfo dirInfo = di.RootDirectory;
Console.WriteLine(dirInfo.Attributes.ToString());
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
foreach (System.IO.FileInfo fi in fileNames)
{
if (fi.Length == 0)
{
fi.Delete();
}
}
System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");
foreach (System.IO.DirectoryInfo d in dirInfos)
{
if (!d.Name.Contains("System Volume Information"))
{
long size = GetDirectorySize(drive + d.Name);
if (size == 0)
{
d.Delete(true);
}
}
}
}
public long GetDirectorySize(string p)
{
string[] a = Directory.GetFiles(p, "*.*", SearchOption.AllDirectories);
long b = 0;
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
b += info.Length;
}
return b;
}
}
}