tags:

views:

723

answers:

7

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?

+12  A: 

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")){...}
nikmd23
thanks, I think this issue has bothered me for years... now I know :)
JL
Is it ok to just have empty - using (File.Create(binaryAddress + @"\LOG"));
JL
For clarity, I'd suggest an explicit close over an empty `using`.
wefwfwefwe
An empty using works, but it doesn't add anything at all over just closing the file.
Guffa
+4  A: 

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();
Guffa
+1  A: 

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.

Joren
+1 for linking to IDisposable information. Learning how to properly implement the IDisposable interface and pattern is, IMO, one of the most important milestones to becoming a better .NET developer. The Framework Design Guidelines (book) is also a good resource on implementing IDisposable.
Yoopergeek
+2  A: 

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)
Yoopergeek
Nice breakdown @yoopergeek ;)
nikmd23
I kinda felt like I was being redundant, but I kinda like knowing some of these extra details, and figured 'why not share?' I also figured I should steer very clear of getting into properly implementing/using IDisposable... :D
Yoopergeek
-1. The code that you are showing is not equivalent to what the using does. The Create call is not done inside the try block, it's made before it. If you don't do anything to the file inside the using block (as the OP explained that he won't), it doesn't add anything at all over just closing the file.
Guffa
Close enough. ;) Instead of just smacking me around, why not provide the correct code? Or a link to MSDN that proves me wrong, such as: http://msdn.microsoft.com/en-us/library/yh598w02.aspx
Yoopergeek
A: 

Does Dispose or Close get called when the method exits?

Chris S
A: 

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.

gimel
A: 
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.

Lion_cl
Under what circumstances does `Dispose` not close the file? As far as I know, it _always_ closes the file.
John Saunders
May be my mistake, of course, but i have tried File.Delete() after using() code and it throw deny exception to me. After placing f.Close() - it allowed me to delete file immediately.I think it may be GC issue, so no one object immediately destructed, just marked to dispose and resource, like physical file is not closed immediately. Anyone has any idea?
Lion_cl