Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File Not Found Exception Handling

File Not Found Exception Handling

Scheduled Pinned Locked Moved C#
csharpdebuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lim Yuxuan
    wrote on last edited by
    #1

    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 ?

    0 1 Reply Last reply
    0
    • L Lim Yuxuan

      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 ?

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • 0 0x3c0

        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

        L Offline
        L Offline
        Lim Yuxuan
        wrote on last edited by
        #3

        thanks it worked well that way.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups