حذف پوشه تکراری
کد:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter path1");
string pth1 = Console.ReadLine();
Console.WriteLine("enter path2");
string pth2 = Console.ReadLine();
string[] dir1 = Directory.GetDirectories(pth1);
string[] dir2 = Directory.GetDirectories(pth2);
string[] fil1 = Directory.GetFiles(pth1);
string[] fil2 = Directory.GetFiles(pth2);
int s = 0,f=0;
///compare directories
for (int i = 0; i < dir1.Length; i++)
{
for (int j = 0; j < dir2.Length; j++)
{
string d1 = dir1[i].Substring(dir1[i].LastIndexOf('\\'));
string d2 = dir2[j].Substring(dir2[j].LastIndexOf('\\'));
if (0==d1.CompareTo(d2))
s++;
}
}
///compare files
for (int i = 0; i < fil1.Length; i++)
{
for (int j = 0; j < fil2.Length; j++)
{
string f1 = fil1[i].Substring(fil1[i].LastIndexOf('\\'));
string f2 = fil2[j].Substring(fil2[j].LastIndexOf('\\'));
if (0 == f1.CompareTo(f2))
f++;
}
}
if (s == dir1.Length && s == dir2.Length && f == fil1.Length && f == fil2.Length)
{
Directory.Delete(pth2, true);
Console.WriteLine("deleted");
Console.ReadKey();
}
else
{
Console.WriteLine("not deleted");
Console.ReadKey();
}
}
}
}