public
partial
class
CompareFolder : Form
{
public
CompareFolder()
{
InitializeComponent();
txtFolderOne.ReadOnly =
true
;
txtFolderTwo.ReadOnly =
true
;
}
/// <summary>
///
/// </summary>
/// <param name="errorList"></param>
private
void
HandleError(List<
string
> errorList)
{
if
(errorList !=
null
&& errorList.Count > 0)
{
ErrorFrom errorForm =
new
ErrorFrom(errorList);
errorForm.Show();
}
}
/// <summary>
///
/// </summary>
/// <param name="messageList"></param>
private
void
HandleMessage(List<
string
> messageList)
{
if
(messageList !=
null
&& messageList.Count > 0)
{
MessageFrom messageForm =
new
MessageFrom(messageList);
messageForm.Show();
}
}
public
List<
string
> ErrorList =
new
List<
string
>();
public
List<
string
> MessageList =
new
List<
string
>();
private
void
btnFolderOne_Click(
object
sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
txtFolderOne.Text = folderBrowserDialog1.SelectedPath;
}
private
void
btnFolderTwo_Click(
object
sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
txtFolderTwo.Text = folderBrowserDialog1.SelectedPath;
}
private
void
btnRun_Click(
object
sender, EventArgs e)
{
if
(
string
.IsNullOrEmpty(txtFolderOne.Text))
{
ErrorList.Add(
"Please Select folder one."
);
HandleError(ErrorList);
return
;
}
if
(
string
.IsNullOrEmpty(txtFolderTwo.Text))
{
ErrorList.Add(
"Please Select folder two."
);
HandleError(ErrorList);
return
;
}
CompareSubDirectory(txtFolderOne.Text, txtFolderTwo.Text);
HandleMessage(MessageList);
HandleError(ErrorList);
}
private
void
CompareSubDirectory(
string
urlone,
string
urltwo)
{
string
pathA =urlone;
string
pathB = urltwo;
System.IO.DirectoryInfo dir1 =
new
System.IO.DirectoryInfo(pathA);
System.IO.DirectoryInfo dir2 =
new
System.IO.DirectoryInfo(pathB);
IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles(
"*.*"
, System.IO.SearchOption.AllDirectories);
IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles(
"*.*"
, System.IO.SearchOption.AllDirectories);
List<
string
> filelistone =
new
List<
string
>();
List<
string
> filelisttwo =
new
List<
string
>();
foreach
(System.IO.FileInfo files
in
list1)
{
string
filepathone = files.DirectoryName.Replace(pathA,
""
) +
"\\"
+ files.Name;
filelistone.Add(filepathone);
}
foreach
(System.IO.FileInfo files
in
list2)
{
string
filepathtwo = files.DirectoryName.Replace(pathB,
""
) +
"\\"
+ files.Name;
filelisttwo.Add(filepathtwo);
}
bool
areIdentical = filelistone.SequenceEqual(filelisttwo);
if
(areIdentical ==
true
)
{
MessageList.Add(
"the two folders are the same.\n"
);
}
else
{
ErrorList.Add(
"The two folders are not the same.\n"
);
}
var queryCommonFiles = filelistone.Intersect(filelisttwo);
if
(queryCommonFiles.Count() > 0)
{
MessageList.Add(
"The following files are in both folders:\n"
);
foreach
(var v
in
queryCommonFiles)
{
MessageList.Add(v +
"\n"
);
}
}
else
{
ErrorList.Add(
"There are no common files in the two folders.\n"
);
}
var queryList1Only = (from file
in
filelistone
select file).Except(filelisttwo);
ErrorList.Add(
"The following files are in list1 but not list2:\n"
);
foreach
(var v
in
queryList1Only)
{
ErrorList.Add(v +
"\n"
);
}
var querylist2Only = (from file
in
filelisttwo select file).Except(filelistone);
ErrorList.Add(
"The following files are in list2 but not list1:\n"
);
foreach
(var v
in
querylist2Only)
{
ErrorList.Add(v+
"\n"
);
}
}