In .Net using File.CreateText() also locks file, why?

.net, filewriter, locking

Solution

Yes it does. `CreateText()` returns a `StreamWriter` object, which is open to the file that you specified.

If you just ignore the return value, that `StreamWriter` hangs around, holding onto your file. You really need to deal with it like in the second block of code.

Problem

I was using the CreateText method to create an empty file (as below) in "App1". Then tried to have another application write to that file but it failed b/c it was locked. It was not unlocked until I closed "App1" ``` File.CreateText(path) ``` To fix this I can do this: ``` Dim sw As StreamWriter = File.CreateText(path) sw.Close() ``` Why does calling just CreateText lock the file? Is there some implicit streamwriter or filewriter or something being created? tep

Original source

Related problems