Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hye It's the first time I'm using c# so I'm not very familiar with it.I have a coding to get the percentage similarity of files. In this program, I have written all the paths of file in notepad which I will compare it with the input file. After I input file and then run, it will compare the input file with a list path of files in notepad and then it will list all the percentage similarity for all the file. I want to know, How I want to create coding to get the highest percentage similarity from the comparison of files? And then it will pop up message that will show the highest percentage from all the list of files.My notepad file name is inputlist.txt. this program have dual mode. first compare between two input file and second is compare input file with path file in notepad. this is my coding :
C#
private void buttonbrowse1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        textBoxfile1.Text = openFileDialog1.FileName;
}

private void buttonbrowse2_Click(object sender, EventArgs e)
{
    if (openFileDialog2.ShowDialog() == DialogResult.OK)
        textBoxfile2.Text = openFileDialog2.FileName;
}

private void buttoncompute_Click(object sender, EventArgs e)
{
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    textBox3.Text = "";
    if (!checkBox1.Checked)
    {
       FileInfo f1 = new FileInfo(textBoxfile1.Text);
       FileInfo f2 = new FileInfo(textBoxfile2.Text);
       if (f2.Length < f1.Length)
       {
           string tmp = textBoxfile1.Text;
           textBoxfile1.Text = textBoxfile2.Text;
           textBoxfile2.Text = tmp;
       }
       Process p = new Process();
       p.StartInfo.FileName = "bsdiff.exe";
       p.StartInfo.Arguments = "\"" + textBoxfile1.Text + "\" \"" + textBoxfile2.Text + "\" diff.bin";



       p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
       p.Start();
       p.WaitForExit();
       FileInfo fi = new FileInfo("diff.bin");


       long lenf3 = f1.Length > f2.Length ? f1.Length : f2.Length;
       long filen = (fi.Length - 140) > 0 ? (fi.Length - 140) : 0;
       float diff = (lenf3 - (filen));

       textBox3.Text = "\r\nDifference of two files:" + (filen) + " bytes ";
       float similar = (diff / lenf3) * 100;
       textBox3.Text += "\r\nPercentage of similarity : " + similar + "%";
       timer1.Stop();
     }
     else
     {
        var lines = File.ReadAllLines("inputlist.txt");
        textBox3.Text += "\r\n\r\nComparing\r\nFile1:" + textBoxfile1.Text+"\r\n----------------------------------\r\n";
        foreach (var line in lines)
        {
           textBoxfile2.Text = line;
           FileInfo f1 = new FileInfo(textBoxfile1.Text);
           FileInfo f2 = new FileInfo(textBoxfile2.Text);
           string file1 = textBoxfile1.Text;
           string file2 = textBoxfile2.Text;
           if (f2.Length < f1.Length)
           {
              string tmp = file1;
              file1 = file2;
              file2 = tmp;
           }
           f1 = new FileInfo(file1);
           f2 = new FileInfo(file2);
           Process p = new Process();
           p.StartInfo.FileName = "bsdiff.exe";
           p.StartInfo.Arguments = "\"" + file1 + "\" \"" + file2 + "\" diff.bin";



           p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
           p.Start();
           p.WaitForExit();
           FileInfo fi = new FileInfo("diff.bin");


           long lenf3 = f1.Length > f2.Length ? f1.Length : f2.Length;
           long filen = (fi.Length - 140)>0?(fi.Length - 140):0;
           float diff = (lenf3 - (filen));


           textBox3.Text += "\r\n\r\n" + file1+","+file2;
           textBox3.Text += "\r\nDifference of two files:" + (filen) + " bytes ";
           float similar = (diff / lenf3) * 100;
           textBox3.Text += "\r\nPercentage of similarity : " + similar + "%";

         }
         timer1.Stop();
     }
  }

private void MainForm_Load(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   groupBox1.Visible = !checkBox1.Checked;
}

private void textBox3_TextChanged(object sender, EventArgs e)
{

}
Posted
Updated 8-Jan-14 23:27pm
v2
Comments
Member 14192083 10-Nov-19 0:36am    
do we import any libary files
Member 14192083 10-Nov-19 0:37am    
what is diff.bin refers

If it's plain text, then Google's diff-match-patch library ought to do what you want (it has a C# version).
google-diff-match-patch

If it's binary data, then look into the things people do to apply updates to executables (bsdiff and Courgette). They look for the minimum difference between two files so that a smaller update can be sent out to end users. Sounds similar to your needs.

Binary diff/patch utility

Courgette
 
Share this answer
 
First things first. Decide which algorithm you want to use that will best suite your needs. A good start will be to look at Levenshtein[+]

Here [+] is a good example of C# code for you to use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900