473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Releasing file locks

I have a maddening problem that I hope someone can help with. I have some
code that writes temporary documents to the data folder of my web site. On
one document, I keep getting an error "The process cannot access the file
'myFile.txt' because it is being used by another process."

Simple failure to wrap a stream writer in a try-catch-finally block, right?
Not so--here's the code in question:

************************************************** *********************************

string filePath = String.Format(@"{0}\Documents\myFile.txt", dataPath);
string msg = String.Empty;
try
{
// Create StreamWriter
streamWriter = new StreamWriter(filePath, false);

// Write file
streamWriter.WriteLine(SomeText);
}
catch (Exception e)
{
// Log error message
msg = e.Message;
}
finally
{
// Close and dispose
if (streamWriter != null)
{
streamWriter.Close();
streamWriter.Dispose();

// Log if the file stream was closed
msg = "File stream closed and disposed";
}
}

// Log result
ActivityLog.WriteEntry(msg, dataPath);

************************************************** *******************

Here's where it gets really strange: We uncovered the problem in testing. We
can replicate it by forcing an exception in another part of the code for
this page. In other words, the original error is not related to the
temporary file in any way, and we have verified that the Close() method for
this file stream is being called long before the original exception is ever
raised. But the next time the page is loaded, we get the "cannot access"
error.

In other words, even though the file stream is properly closed when the file
is created, on the next page load, it is locked, causing a "cannot access"
exception.

Has anyone come across anything like this? I am totally baffled. Any
suggestions as to how to proceed would be appreciated.

David Veeneman
Foresight Systems
Feb 14 '07 #1
1 1397
The short answer: Look for locks in other parts of the program, which is
where I found my solution.
Feb 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike King | last post by:
How can we get ASP.NET/CLR to release a native DLL when using P/Invoke. We are building a web application that uses a native DLL heavily. It's unlikely that we will ever have a Managed DLL...
2
by: Oenone | last post by:
I've an ASP.NET 2.0 project into which various Plug-In DLLs can be installed. I'm running it in the ASP.NET Development Server. In order to load one of the PlugIn DLLs I'm using...
1
by: John Wright | last post by:
During my program I load an exe file using reflection. My program loads the file using reflection, checks the assembly version and does an update if the network version is different from the local...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
0
by: David Veeneman | last post by:
I have a maddening problem that I hope someone can help with. I have some code that writes temporary documents to the data folder of my web site. On one document, I keep getting an error "The...
2
by: David Veeneman | last post by:
My web app creates several temporary files, then attaches them to an email that it sends out. When the app attches the documents to the email, .NET locks the files. My problem is that they aren't...
3
by: fperri | last post by:
I have a DB in Access 2002 that when it is ran on a few users computers in the company, they get the error that the file sharing lock count has been exceeded and to increase the max locks per file in...
6
by: Bill H | last post by:
I am wondering if this is possible. I have a file that is accessed by multiple users and keeps track of activity (the file is polled by flash every 2 seconds). As users leave, the flash program...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.