File Not Found Exception Handling
-
Hi I am trying to handle a file not found exception by creating the specified text file if it does not exists. void RetrieveData() { if(File.Exists("books.txt") == false) // if file doesnt exists { File.Create("books.txt"); // create the file } //Create a stream reader object to read from "books.txt" StreamReader DataReader = new StreamReader("books.txt"); } I did a test run and remove the books.txt file from the current working directory on purpose and when i start debugging all I get is this exception : The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\NYP Work\C#\MiniProject 2009\PhotoAlbum_V2\bin\Debug\books.txt' because it is being used by another process. Any suggested solutions ?
-
Hi I am trying to handle a file not found exception by creating the specified text file if it does not exists. void RetrieveData() { if(File.Exists("books.txt") == false) // if file doesnt exists { File.Create("books.txt"); // create the file } //Create a stream reader object to read from "books.txt" StreamReader DataReader = new StreamReader("books.txt"); } I did a test run and remove the books.txt file from the current working directory on purpose and when i start debugging all I get is this exception : The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\NYP Work\C#\MiniProject 2009\PhotoAlbum_V2\bin\Debug\books.txt' because it is being used by another process. Any suggested solutions ?
Your problem lies with the File.Create method. It returns a FileStream instantiation. Until you close the FileStream, you cannot read the file without requesting different access to it. Try something like this
void RetrieveData()
{
Stream bookStream = new FileStream("books.txt", FileMode.OpenOrCreate);using(StreamReader booksReader = new StreamReader(bookStream)) { //Do what you want with booksReader here, the using block will automatically close the streams to avoid memory leaks }
}
The trick here is creating a new FileStream with FileMode.OpenOrCreate. If the file exists, it is opened; if it doesn't exist, it is created and then opened. If you want to check whether the file is empty (it's pointless reading from an empty file), check whether bookStream.Length is equal to zero. This will also tell you if the file is newly created because the newly created file will be empty
-
Your problem lies with the File.Create method. It returns a FileStream instantiation. Until you close the FileStream, you cannot read the file without requesting different access to it. Try something like this
void RetrieveData()
{
Stream bookStream = new FileStream("books.txt", FileMode.OpenOrCreate);using(StreamReader booksReader = new StreamReader(bookStream)) { //Do what you want with booksReader here, the using block will automatically close the streams to avoid memory leaks }
}
The trick here is creating a new FileStream with FileMode.OpenOrCreate. If the file exists, it is opened; if it doesn't exist, it is created and then opened. If you want to check whether the file is empty (it's pointless reading from an empty file), check whether bookStream.Length is equal to zero. This will also tell you if the file is newly created because the newly created file will be empty
thanks it worked well that way.