Solved

Windows File Locks Interfering with C# StreamWriter

Posted on 2012-04-05
2
1,388 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
Comment Utility
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
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now