JL.
JL.

Reputation: 81262

.NET File.Create , can't delete file afterwards

Using method: System.IO.File.Create()

After the file gets created, it still remains used by a process, and I can't delete it.

Any idea how I can better create the file, should be a 0byte file, and then somehow close and dispose?

Upvotes: 12

Views: 17474

Answers (6)

Victor Gelmutdinov
Victor Gelmutdinov

Reputation: 489

using(FileStream f = File.Create(file_path))
{
  // ... do something with file
  f.Close();
}

The "f.Close();" line closing file immediately. If not close manually, Disposing may not close it.

Upvotes: 1

Yoopergeek
Yoopergeek

Reputation: 5642

nikmd23 has the short answer, the long answer is: the FileStream that File.Create(...) is returning is not being deterministically disposed of, therefore it's file handle is not closed when you're trying to delete it.

As nikmd23 put it, wrapping your File.Create(...) call will with a using statement will make sure the stream is closed and disposed of:

using (FileStream fs = File.Create(path)) { 
  // do anything with the stream if need-be...
}
File.Delete(path); //after it's been disposed of.

The using(...) block is really just compiler-sugar for:

FileStream fs = File.Create(path);
try { 
   // do anything with the stream if need-be...
}
finally { 
  fs.Dispose();
}
File.Delete(path)

Upvotes: 12

gimel
gimel

Reputation: 86344

See System.IO.File.Create(String) Method paramter and return value description

Parameters

path Type: System.String The path and name of the file to create.

Return Value

Type: System.IO.FileStream

A FileStream that provides read/write access to the file specified in path.

The FileStream return value is there for IO access to the created file. If you are not interested in writing (or reading) the newly created file, close the stream. That is what the using block is ensuring.

Upvotes: 0

Joren
Joren

Reputation: 14746

You should use nikmd23's answer in almost all cases. If you can't, because you need to pass the FileStream somewhere else, make sure to call the FileStream.Close method eventually. Preferably you would have the class that 'owns' the FileStream implement IDisposable itself, and close the stream in its Dispose method.

For more information on implementing IDisposable, refer to the MSDN documentation. Easier reading, and more up to date, is Joe Duffy's post on the subject.

Upvotes: 1

Guffa
Guffa

Reputation: 700152

The Create method not only creates the file, it opens it and return a FileStream object that you can use to write to the file.

You have to close the file after yourself, otherwise it will not be closed before the garbage collector cleans up the FileStream object.

The easiest way is to simply close the file using the reference that the Create method returns:

File.Create(fileName).Close();

Upvotes: 16

nikmd23
nikmd23

Reputation: 9103

JL,

You should wrap your call to .Create in a using statement so that the FileStream that .Create returns will be closed properly. IE:

using (File.Create("path")){...}

Upvotes: 29

Related Questions