Solved

Windows File Locks Interfering with C# StreamWriter

Posted on 2012-04-05
2
1,425 Views
Last Modified: 2012-04-06
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.
                                          Thanks!
                                                  Joyce
StreamWriter.doc
0
Comment
Question by:oneDayAtaTime
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37812079
Your code looks good.  The using on StreamWriter takes care of releasing (flush/close/dispose) the file as expected.  

Is it possible that the user has the file open on another application when triggering the HTML File Generation?
0
 

Author Comment

by:oneDayAtaTime
ID: 37816461
YES, thank you!!!! I now think I was looking in the wrong place. Once I create the file, I have a Windows Service that picks it up and sends it via email. It is also in C#, and I think THAT is where the file-locking is happening. I've added a "email.Attachments.Dispose();" line that will perform clean-up after the email is sent. Hopefully this will correct my problem. Thank you for helping me to see the problem in a different light! Also thanks for verifying that the "using" block performs clean-up.
                              Joyce
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question