PDA

View Full Version : حذف داده ها با پسوند خاص



fakhravari
یک شنبه 12 مرداد 1393, 17:55 عصر
private void button3_Click(object sender, EventArgs e)
{
ListBox listBox1 = new ListBox();


DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Stack<DirectoryInfo> dirstack = new Stack<DirectoryInfo>();
dirstack.Push(new DirectoryInfo(d.Name));
while (dirstack.Count > 0)
{
try
{
DirectoryInfo current = dirstack.Pop();
foreach (DirectoryInfo q in current.GetDirectories())
{
if ((q.Attributes & FileAttributes.System) != FileAttributes.System)
{
string[] Paswand = new string[22] { "*.jpg", ".avi", ".mpg", ".mov", ".docx", "doc", ".png", ".gif", ".jpeg", ".html", ".htm", ".tif", ".bmp", ".mp4", ".ts", ".3gp", ".mkv", ".flv", ".swf", ".wmv", ".mpeg", ".divx" };
string[] Arry = Directory.GetFiles(q.FullName, "*.*", System.IO.SearchOption.AllDirectories);




string[] Arry2 = Directory.GetFiles(d.Name, "*.*");
for (int i = 0; i < Paswand.Length; i++)
{
for (int b = 0; b < Arry2.Length; b++)
{
string Ex = Path.GetExtension(Arry2[b]).ToLower();
if (Paswand[i] == Ex)
{
if (!listBox1.Items.Contains(Arry2[b]))
listBox1.Items.Add(Arry2[b]);
}
}
}








for (int i = 0; i < Paswand.Length; i++)
{
for (int b = 0; b < Arry.Length; b++)
{
string Ex = Path.GetExtension(Arry[b]).ToLower();
if (Paswand[i] == Ex)
{
if (!listBox1.Items.Contains(Arry[b]))
listBox1.Items.Add(Arry[b]);
}
}
}




for (int t = 0; t < listBox1.Items.Count; t++)
{
try
{ File.Delete(listBox1.Items[t].ToString()); }
catch { }
}
}
}
}
catch { }
}
}
}


با سلام
کد من را بهینه کنید

fakhravari
دوشنبه 13 مرداد 1393, 19:46 عصر
یه جواب!!:متفکر: