0

In my c# application which developed with c# in visual studio 2012 I created a file by this command : System.IO.File.Create("config.conf"); after that in the next line I want to use the file by this command : System.IO.StreamReader rd = new System.IO.StreamReader("config.conf"); But I get This exception :

"The process cannot access the file '\config.far' because it is being used by >another process."

I used thread.sleep(2000) to make application wait but still it doesn't answer.

I will appropriate any help.

Majid
  • 39
  • 1
  • 11
  • The problem here is not the *this* code, but the code that in some way accessed config.confg and left it open – Tigran Apr 29 '14 at 10:52
  • 1
    The problem here **is** in this code. `File.Create` returns an open `FileStream` for the newly created file. Until GC collects (and thus calls the finalizer of) that stream, the file will be left open. – Lasse V. Karlsen Apr 29 '14 at 10:54
  • possible duplicate of [Why is File.Create needed to be closed?](http://stackoverflow.com/questions/6072310/why-is-file-create-needed-to-be-closed) – Lasse V. Karlsen Apr 29 '14 at 10:55

5 Answers5

5

File.Create creates the file and returns a FileStream holding the file open.

You can do this:

System.IO.File.Create("config.conf").Dispose();

by disposing of the returned stream object, you close the file.

Or you can do this:

using (var stream = File.Create("config.conf"))
using (var rd = new StreamReader(stream))
{
    .... rest of your code here

Additionally, since disposing of the StreamReader will also dispose of the underlying stream, you can reduce this to just:

using (var rd = new StreamReader(File.Create("config.conf")))
{
    .... rest of your code here

Final question: Why are you opening a newly created stream for reading? It will contain nothing, so there's nothing to read.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1
using(var conf = System.IO.File.Create("config.conf"))
{
    using (var rd = new System.IO.StreamReader(conf))
    {
        // Do whatever you want to do with the file here
    }

}
Onur Okyay
  • 190
  • 1
  • 10
0

The problem is that File.Create returns a stream to the file. That is: The file is already opened for you!

You could do this:

using (System.IO.StreamReader rd = new System.IO.StreamReader(System.IO.File.Create("config.conf")))
{
   ...
}

By the way, this does not really make sense. What do you expect an empty, newly created file to contain?

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

When working with files, it is always a good idea to dispose of the file once you are done.

This can be done by two different techniques, the most popular one is using a "using" statement:

using (FileStream fileStream = File.Create(fileNamePath))
{
    // insert logic here, for example:
    fileStream.SetLength(fileSize);
}

The other one, is calling the .Dispose method.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
-3

Close the file if it is opened in notepad or something similar.

leskovar
  • 661
  • 3
  • 8