Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
if (File.Exists(path))
{
    readFile(path);  // Reading File Here... Executing correctly
    if (File.Exists(path))
    {
        File.Delete(path);  // Error Comes Here
    }
}
Posted
Updated 30-Nov-10 0:45am
v3

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!
 
Share this answer
 
v2
Comments
Dalek Dave 30-Nov-10 6:43am    
Good Call.
Yatin Bhagat 30-Nov-10 6:48am    
how to set the file permissions in c#.net....my file is stored in application root directory
Toli Cuturicu 30-Nov-10 6:51am    
Ok. So we found the problem. Don't ever again store a user file in the application root directory! Never!
You have LocalApplicationData for this!
Yatin Bhagat 30-Nov-10 6:58am    
i am creating xml file and stored it in debug folder...and after that from this location i am trying to read the file.read operation executes without any error but after that i am trying to delete this file and here comes the error
Yatin Bhagat 30-Nov-10 23:45pm    
and even i am not getting Dispose() method for reader
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.
 
Share this answer
 
v2
use the following code for loading the picture in imagebox

//////
pictureBox1.Load(filePath);
 
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