删除文件时出错(读取后) [英] Error While Deleting File (after being read)

查看:83
本文介绍了删除文件时出错(读取后)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
在我的应用程序中,我正在读取一个xml文件来加载数据.我已经使用XmlReader来读取文件.正确读取文件.读取文件后,我正在删除文件,但是它给出了如下错误:

"System.IO.IOException:该进程无法访问文件〜\ PurchaseBill.xml",因为它被另一个进程使用"

我关闭了读者.我的代码就像下面这样:

Hello,
In my application I am reading an xml file for loading data. I have used XmlReader for reading the file. File is read correctly. After reading the file, I am deleting the file but it gives error like:

"System.IO.IOException: the process cannot access the file "~\PurchaseBill.xml" because it is used by another process"

I have closed the reader. My code is like bellow:

if (File.Exists(path))
{
    readFile(path);  // Reading File Here... Executing correctly
    if (File.Exists(path))
    {
        File.Delete(path);  // Error Comes Here
    }
}

推荐答案

文件仍被您的进程锁定.
您必须释放它的句柄.
密钥在您的void readFile(string)方法中.
在那里,您必须Dispose()您正在使用的任何资源.

...或者,也许,您只是没有删除文件的权限.
检查文件的权限,然后尝试以管理员身份运行该程序.无论如何,我希望该文件不在Program Files中,并且您正在使用Vista或Seven !!
The file remains locked by your process.
You have to free its handle.
The key is in your void readFile(string) method.
There, you must Dispose() whatever resource you are using.

... Or, maybe, you just don''t have the permission to delete the file.
Check the file''s permissions and try to run the program as an administrator. Anyway, I hope that the file is not located in Program Files and you are using Vista or Seven!


请确保在阅读后关闭文件.

将来请注意,如果您使用复杂的代码并且对文件进行了很多操作,也许使用了无进程,最后您已将此文件锁定,但您不知道哪个进程有罪.有一些免费软件可以帮助您了解为什么文件被锁定,对不起,我无法回忆起软件的专有名称,但是您可以在需要时进行搜索.
Make sure u close file after reading.

Note for future, if u have complex code and u made a lot of operations with file, using nany processes maybe and finally u have this file locked but u dont know what process is guilty. There are some free software that help you to find out why your file is locked, sorry i cant recall exect name of software but u can search for it if needed.


使用以下代码在imagebox中加载图片

//////
pictureBox1.Load(filePath);
use the following code for loading the picture in imagebox

//////
pictureBox1.Load(filePath);


这篇关于删除文件时出错(读取后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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