This application is going to display folder contents and also compare the contents of two folders. I am using a web application to display folder contents. By using this application, we can compare two folders easily. This application will display files not common to both folders.
Creating the web application in C#
First, create a web application in c#, and name it Folderviewer. Next import System.IO namespace. Next, create UI as shown below. Here I used 4 listboxes, 2 textboxes, 1 button, and some labels.
Button click event and code
Next Button_click event and copy this code
try
{
// To Load files into ListBox1
ListBox1.Items.Clear();
string path = TextBox3.Text;
foreach (string f in Directory.GetFiles(path))
{
string filename = f.Substring(f.LastIndexOf(@"\") + 1);
string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
ListBox1.Items.Add(fn_withoutextn.ToString());
}
ListBox2.Items.Clear();
// To Load files into ListBox2
fillsecond();
// To show files that are present in ListBox1 but not in ListBox2
showdiffA();
// To show files that are present in ListBox2 but not in ListBox1
showdiffB();
Label5.Text = string.Empty;
}
catch (System.Exception ex)
{
Label5.Text = (ex.Message).ToString();
}
// Definition for method fillsecond is:
private void fillsecond()
{
foreach (string f in Directory.GetFiles(TextBox4.Text))
{
string filename = f.Substring(f.LastIndexOf(@"\") + 1);
string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
ListBox2.Items.Add(fn_withoutextn.ToString());
}
}
// Definition for method showdiffA is:
private void showdiffA()
{
ListBox3.Items.Clear();
foreach (string f in Directory.GetFiles(TextBox3.Text))
{
if (!File.Exists(TextBox4.Text + f.Substring(f.LastIndexOf(@"\") + 1)))
{
string filename = f.Substring(f.LastIndexOf(@"\") + 1);
string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
ListBox3.Items.Add(fn_withoutextn);
}
}
Label3.Text = "Files not present in " + TextBox4.Text;
}
// Definition for method showdiffB is:
private void showdiffB()
{
ListBox4.Items.Clear();
foreach (string f in Directory.GetFiles(TextBox4.Text))
{
if (!File.Exists(TextBox3.Text + f.Substring(f.LastIndexOf(@"\") + 1)))
{
string filename = f.Substring(f.LastIndexOf(@"\") + 1);
string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
ListBox4.Items.Add(fn_withoutextn);
}
}
Label4.Text = "Files not present in " + TextBox3.Text;
}
Running the application
Run the application, specify the folder path in each TextBox, and click the Show button.
It will display files not common to both folders as shown below.