Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code like as:

C#
private void button1_Click(object sender, EventArgs e)
       {
           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.CheckFileExists = true;
           openFileDialog.AddExtension = true;
           openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
           DialogResult result = openFileDialog.ShowDialog();
           if (result == DialogResult.OK)
           {
               filename = Path.GetFileName(openFileDialog.FileName);
               path = Path.GetDirectoryName(openFileDialog.FileName);
               textBox1.Text = path + "\\" + filename;
           }
       }

       private void button2_Click(object sender, EventArgs e)
       {

           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.CheckFileExists = true;
           openFileDialog.AddExtension = true;
           openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
           DialogResult result = openFileDialog.ShowDialog();
           if (result == DialogResult.OK)
           {
               filename = Path.GetFileName(openFileDialog.FileName);
               path = Path.GetDirectoryName(openFileDialog.FileName);
               textBox2.Text = path + "\\" + filename;
           }
       }


        unsafe static long DumbDifference(string file1Path, string file2Path)
       {
           MemoryMappedFile file1 = MemoryMappedFile.CreateFromFile(
                    file1Path, System.IO.FileMode.Open,
                    null, 0, MemoryMappedFileAccess.Read);
           MemoryMappedFile file2 = MemoryMappedFile.CreateFromFile(
                    file2Path, System.IO.FileMode.Open,
                    null, 0, MemoryMappedFileAccess.Read);
           MemoryMappedViewAccessor view1 = file1.CreateViewAccessor();
           MemoryMappedViewAccessor view2 = file2.CreateViewAccessor();

           long length1 = checked((long)view1.SafeMemoryMappedViewHandle.ByteLength);
           long length2 = checked((long)view2.SafeMemoryMappedViewHandle.ByteLength);
           long minLength = Math.Min(length1, length2);

           byte* ptr1 = null, ptr2 = null;
           view1.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr1);
           view2.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr2);

           ulong differences = (ulong)Math.Abs(length1 - length2);

           for (long i = 0; i < minLength; ++i)
           {
               // if you expect your files to be pretty similar,
               // you could optimize this by comparing long-sized chunks.
               differences += ptr1[i] != ptr2[i] ? 1u : 0u;
           }

          long l=checked((long)differences);;
            return l;
       }

       private void button3_Click(object sender, EventArgs e)
       {
         long  l = DumbDifference(textBox1.Text, textBox2.Text);
           label1.Visible = true;
           label1.Text = Convert.ToString(l);
       }



my application theme is compare binary files and find the difference in terms of

percentage.in this process error occured in method-DumbDifference .

error statment like as:

C#
MemoryMappedViewAccessor view1 = file1.CreateViewAccessor();


Finally error is " system.unauthorizedaccessexception access to the path is denied"

please help me.

thank u.
Posted

1 solution

Regarding your question: Try using this overload of the CreateViewAccessor[^]-Method and specify Read-Access there as well and please use a using-Block, like so:
C#
using (var view1 = file1.CreateViewAccessor(0, file1.Length, MemoryMappedFileAccess.Read))
{
    // access the view
}

Also use a using-Block for MemoryMappedFile file1 = MemoryMappedFile.CreateFromFile ...

Regarding your overall intention: The last question you asked was about a OutOfMemoryException because you used the Levenshtein-Distance-Algorithm to compare PDF's. Now you're trying to compare them byte-by-byte. Two questions for you:
- Why you would need a MemoryMappedFile for this approach?
- What are you actually trying to achieve?
 
Share this answer
 
Comments
Krishna Veni 25-Mar-15 15:04pm    
ya.now i am try to compare byte to byte.i want to percentage whenever compare the pdf's
Sascha Lefèvre 25-Mar-15 15:07pm    
Percentage... of similarity? You won't get a meaningful result then with a byte-by-byte comparison.

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