File Create and then cannot StreamReader

This is a discussion on File Create and then cannot StreamReader within the C# Programming forums, part of the General Programming Boards category; Hi, When my app start it checks if certain file in a certain directory exist and if they don't exist ...

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    Arrow File Create and then cannot StreamReader

    Hi,
    When my app start it checks if certain file in a certain directory exist and if they don't exist creates them.
    Later my app tries to read from that file but an exception is thrown:
    System.IO.IOException: The process cannot access the file 'c:\test\a.txt' because it is being used by another process. and it goes on and on with this message.
    I have written a small test program to show what I do and what is my problem.
    Code:
             try 
                { 
                    if (System.IO.Directory.Exists("c://test") == false) 
                        System.IO.Directory.CreateDirectory("c://test"); 
                    if (System.IO.File.Exists("c://test") == false) 
                        System.IO.File.Create("c://test//a.txt"); 
     
                    using (System.IO.StreamReader sr = System.IO.File.OpenText("c://test//a.txt")) 
                    { 
                        System.Windows.MessageBox.Show("success!"); 
                    } 
                } 
                catch (Exception e) 
                { 
                    System.Windows.MessageBox.Show(e.ToString(), "Why??"); 
                    throw; 
                }
    What I am doing wrong? Please.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    System.IO.File.Create also opens the file, and since you don't explicitly close it it *may* still be open when you call System.IO.File.OpenText, thus the exception.
    Also, not sure why you use double front-slashes, but it seems to work, so whatever .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    I suspected that but could not find a way to close the file opened with File.Create.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Create returns a file handle (a FileStream object), so store the return value and use that to close.

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    smack myself on the head.
    thanks works good now.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    tbh i find this easier:

    Code:
    if(!File.exists("c:\\test\\a.txt"))
    {
      using(StreamWriter myWriter = new StreamWriter("c:\\test\\a.txt"))
      {
        // creates it if it doesn't exist, even the folder
      }
    }
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  7. #7
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by DanFraser View Post
    tbh i find this easier:

    Code:
    if(!File.exists("c:\\test\\a.txt"))
    {
      using(StreamWriter myWriter = new StreamWriter("c:\\test\\a.txt"))
      {
        // creates it if it doesn't exist, even the folder
      }
    }
    I see what you mean but it wont do for what I need.
    I just need to read from the file and I create it so I wont have to worry if it exist.
    I am only writing to the file later.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    When my app start it checks if certain file in a certain directory exist and if they don't exist creates them.
    That little section does exactly what it says on the tin.

    Later my app tries to read from that file but an exception is thrown
    It solves this by the using statement closing access to the file immediately.

    Later on in your code when you want to read/write to the file, just do it in a normal way. No access problems, and the code is very short too.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21