unauthorizedaccessexception拒绝访问该

unauthorizedaccessexception拒绝访问该

本文介绍了解决“ system.unauthorizedaccessexception拒绝访问该路径“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如:



my code like as:

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);
       }







我的应用主题是比较二进制文件并找到



percentage.in此过程在方法-DumbDifference中出错。



错误陈述如:






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:

MemoryMappedViewAccessor view1 = file1.CreateViewAccessor();





最后错误是system.unauthorizedaccessexception拒绝访问路径



请帮帮我。



谢谢你。



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

please help me.

thank u.

推荐答案

using (var view1 = file1.CreateViewAccessor(0, file1.Length, MemoryMappedFileAccess.Read))
{
    // access the view
}



还使用了用于MemoryMappedFile文件1的using-Block = MemoryMappedFile.CreateFromFile ...



关于你的整体意图:你问的最后一个问题是关于OutOfMemoryException,因为你使用Levenshtein-Distance-Algorithm来比较PDF。现在你要逐个字节地比较它们。两个问题:

- 为什么你需要一个MemoryMappedFile用于这种方法?

- 你实际想要实现什么?


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?


这篇关于解决“ system.unauthorizedaccessexception拒绝访问该路径“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:33