I have a Windows application written in C# that utilizes StreamWriter to create an HTML file. It grabs the HTML from a table and creates a file from it. My issue is that on some occasions, the application user has the need to create the file within minutes of initially creating the file. Sometimes when this happens, the file is "locked" by Windows and won't allow StreamWriter to write to it. Most times it seems to release the lock right away, but not every time.
It seems this is more a Windows problem than a StreamWriter problem. If I could find a way to manually unlock the file (through C#), if it's locked, that would prevent the hang-up. But I'm not sure that's possible.
I've attached the code that creates the file. I've enclosed my StreamWriter call within a "using" block, because that is supposed to dispose of all unmanaged resources. I don't know what else I can do from my end, to ensure the file doesn't remain locked.
Any ideas on how to keep my code from hanging because of a locked file? It's weird because if I have the file open, it still writes to it (so is apparently not locked). I don't know why the file is locked sometimes after creation.