Solved

Windows File Locks Interfering with C# StreamWriter

Posted on 2012-04-05
2
1,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

734 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