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?

phoenix
  • 7,988
  • 6
  • 39
  • 45
Chris
  • 3,191
  • 4
  • 22
  • 37

6 Answers6

121

Try this:

System.IO.File.Create(FullFName).Close();
Elmo
  • 6,409
  • 16
  • 72
  • 140
joe
  • 1,211
  • 1
  • 8
  • 2
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
}
Elmo
  • 6,409
  • 16
  • 72
  • 140
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • 2
    I'd have never ever expected that. – ASA Jul 04 '16 at 19:14
  • 1
    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.1 – Dag Jan 18 '19 at 10:26
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))
{
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
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.

Kev
  • 118,037
  • 53
  • 300
  • 385
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.

polfosol ఠ_ఠ
  • 1,840
  • 26
  • 41
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
malay
  • 1,434
  • 4
  • 17
  • 28