Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is File.Create needed to be closed?

Tags:

c#

.net

The following throws an exception "The process cannot access the file 'D:\MyDir\First.txt' because it is being used by another process."

static void Main(string[] args)
{
    Directory.CreateDirectory(@"D:\MyDir");
    File.Create(@"D:\MyDir\First.txt");
    File.WriteAllText(@"D:\MyDir\First.txt", "StackOverflow.com");
}

However following works:

using (File.Create(@"D:\MyDir\First.txt"))
{ 
}

or

File.Create(@"D:\MyDir\First.txt").Close();

Why? What in File.Create needs to be closed?

like image 757
Kang Avatar asked May 20 '11 13:05

Kang


People also ask

Do you need to close files in C#?

Yes, here it would be required so that the unmanaged resources were cleaned up. Show activity on this post. It's not necessary because Dispose() will close the underlying file. However since that's an implementation detail it's good practice to explicitly call Close() when you're done with the stream.

How do you close a file in C#?

Use it like this File. Create(myPath). Close(); .

Does StreamWriter create file C#?

C# StreamWriter append text This constructor initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, the constructor creates a new file.

How do you create a file in C sharp?

The Create() method of the File class is used to create files in C#. The File. Create() method takes a fully specified path as a parameter and creates a file at the specified location; if any such file already exists at the given location, it is overwritten.


4 Answers

File.Create is doing more than you think here. It's not just creating the file, it's also returning an active stream to the file. However, you're not doing anything with that stream. The using block in your latter example closes that stream by disposing it.

Note also that this is a significant clue about the return value:

File.Create(@"D:\MyDir\First.txt").Close();

(It actually wasn't intuitive to me when I first read your question, but looking back at it this line of code actually says it all.)

Your next step, calling File.WriteAllText also does more than you think. According to the documentation, it:

Creates a new file, writes the specified string to the file, and then closes the file.

So it would seem that your File.Create call isn't really needed here anyway.

like image 164
David Avatar answered Sep 30 '22 17:09

David


Because it opens a file stream, which is a class managing some operating system low-level resources and those must be released in order to let other operations in other threads, and even in other applications, access to the file.

like image 44
Matías Fidemraizer Avatar answered Oct 04 '22 17:10

Matías Fidemraizer


You don't actually need to call File.Create() to then be able to call File.WriteAllText().

File.WriteAllText() will create a new file and write to it then close the file all in one handy method.

If the file already exists it'll be overwritten.

like image 11
Kev Avatar answered Oct 04 '22 17:10

Kev


The MSDN docs for File.Create() explain this:

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.

Basically until the file create is closed the file cannot be access by another process (in this case your attempt to write to it).

like image 10
Dan Diplo Avatar answered Oct 04 '22 17:10

Dan Diplo