That's what I think. when I copied and ran your code, an exception was thrown. This is probably due to the fact that you create your file twice and do not close it before creating it a second time.
For reference, TextWriter tw = new StreamWriter(path); creates a file for you. You do not need to call File.Create
and during subsequent runs, I donβt think you are deleting the file, and since the file already exists, if (!File.Exists(path)) will never be satisfied and the whole if will be skipped
So there are a few points
- get rid of this call to
File.Create - If you want the file to be overwritten, you should not check if it exists, you should simply overwrite.
source share