访问路径'file xxx'被拒绝。 [英] Access to the path 'file xxx' is denied.

查看:114
本文介绍了访问路径'file xxx'被拒绝。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从驱动器中读取一个文件,但是在读取文件时会出错。

以下是我的代码:

I would like to read a file from the drive, but it gives me an error while reading the file.
Here is my code below:

lblLastReal.Text = e.FullPath;
listBox3.Items.Add(lblLastReal.Text);
openFileDialog1.FileName = "";
TextBox txtScanBox = new TextBox();

StreamReader stread = new StreamReader(System.IO.Directory.GetCurrentDirectory() + @"\abc.txt");
txtScanBox.Text = stread.ReadToEnd();
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
GC.Collect();

File.SetAttributes(e.FullPath, FileAttributes.ReadOnly);
FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x2000);
fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x2000);
MD5.ComputeHash(fs);
byte[] hash = MD5.Hash;
StringBuilder buffer = new StringBuilder();
byte hashByte = 0;
foreach (byte hashByte_loopVariable in hash)
{
    hashByte = hashByte_loopVariable;
    buffer.Append(string.Format("{0:X2}", hashByte));
}
fs.Close();



有时它会给我错误进程无法访问该文件,因为它正被另一个进程使用


and sometimes it gives me error "The process cannot access the file because it is being used by another process"

推荐答案

第二个错误:进程无法访问该文件,因为它正由另一个进程使用

尝试关闭StreamReader stread - 因为你只是让它超出范围,它只会在垃圾收集器决定有必要删除它时关闭。



第一个问题可能是一个权限问题 - 不知道你的代码片段中有什么e,我不能确切地说,但很可能你的应用程序运行的任何用户都没有你所需的文件夹权限正在尝试设置文件属性。
The second error first: "The process cannot access the file because it is being used by another process"
Try closing your StreamReader stread - since you just let it go out of scope, it will only be closed when the Garbage Collector decides it is necessary to remove it.

The first problem is probably a permissions issue - without knowing what "e" is in your code fragment, I can''t say exactly, but the chances are that whatever user your application is running as does not have the required permissions for the folder you are trying to set the file attributes on.


thnx为你的建议,

i检查了所有关闭fs(streamreader)的可能性但是没有工作

e是filesystemwatc的变量她改变了事件。
thnx for ur suggestion,
i have checked with all posibility of closing fs(streamreader) but didn''t work
e is the variable of filesystemwatcher changed event.


这篇关于访问路径'file xxx'被拒绝。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆