System.IO.File.Create locking a file
Asked Answered
A

6

61

I'm using System.IO.File.Create to create a file. I'm not writing to it with a stream writer, just creating it.

I get a server error in the front end when the application tries to open the newly created file - that the file is in use. Garbage collection then seems to come along and a few minutes later all is OK.

Now I know if I was using Streamwriter I would have to close it. Does the same apply to creating?

I've read that opening a stream writer to the file then immediately closing it will fix this, but it seems messy. Is there a simpler way?

Arnhem answered 13/1, 2011 at 12:55 Comment(0)
R
84

File.Create returns a FileStream. You should use it like this:

using (FileStream fs = File.Create(path))
{
    //you can use the filstream here to put stuff in the file if you want to
}
Rabid answered 13/1, 2011 at 13:0 Comment(2)
I'd have never ever expected that.Zolnay
Yes, we all thought so. But the using statement does not work, not even if I put in fs.Close() at the end. The file still hangs. I am using .NET 4.6.1Denishadenison
F
122

Try this:

System.IO.File.Create(FullFName).Close();
Fainthearted answered 16/2, 2012 at 16:45 Comment(5)
This is perfect for when you just want to create a blank file.Dalia
That is so true! "I'm not writting to it with a stream writer, just creating it".Hammy
This was what I was looking for as well. Works a charm with a blank file. Thanks.Nobell
Not a biggy, but this violates the disposable pattern. I would replace Close() with Dispose() (which also calls Close).Iggy
"I would replace Close() with Dispose()" or better yet, with a using statementTesstessa
R
84

File.Create returns a FileStream. You should use it like this:

using (FileStream fs = File.Create(path))
{
    //you can use the filstream here to put stuff in the file if you want to
}
Rabid answered 13/1, 2011 at 13:0 Comment(2)
I'd have never ever expected that.Zolnay
Yes, we all thought so. But the using statement does not work, not even if I put in fs.Close() at the end. The file still hangs. I am using .NET 4.6.1Denishadenison
K
12

Creating the file opens a FileStream to it, hence, locking it (File.Create returns the FileStream).

You must close this stream in order to access the file. This is best done with a using statement:

using(FileStream fs = File.Create(path))
{
}
Kenji answered 13/1, 2011 at 13:0 Comment(0)
D
9

When using File.Create you get a FileStream returned. Until you either close the stream or until the FileStream object is disposed (by the garbage collector's finaliser) it will remain open and locked.

FileStream implements IDisposable so you can do the following:

using(FileStream fs = File.Create(filename))
{
    // Do stuff like write to the file
}

The using statement is "syntactic sugar" and causes the compiler to generate code that is functionally equivalent to:

FileStream fs = File.Create(filename)
try
{
    // Do stuff like write to the file
}
finally
{
    fs.Dispose();
}

The Dispose method calls Close internally.

Dunkin answered 13/1, 2011 at 13:15 Comment(0)
D
3

I was using: System.IO.File.Create(sFullFileName);

The above .Create method was not closing the file

I now use: System.IO.File.WriteAllText(sFullFileName, "Uploading");

This method creates and closes the file (note: I put a string "Uploading" in the file, but i'm sure string.Empty will also work.

Dusky answered 19/12, 2011 at 22:45 Comment(0)
R
1

The Create method will return a file handle. The file handle should be closed before re-using the file. Please see details in the MSDN article File.Create Method (String).

Summary:

The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.

Rejuvenate answered 13/1, 2011 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.