473,396 Members | 2,129 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,396 software developers and data experts.

FileStream resource locking

GDL
Hi,

I'm using a FileStream as below:

FileStream fs = new FileStream
(
filePath,
FileMode.Create,
FileAccess.Write,
FileShare.None
);

//do some writing
fs.Write(...)

//close
fs.Close();

My problem is that even after the stream is closed no other applications can
access the file (resource in use) until I close the application which created
it.

Can anybody help with this please?

--
Many Thanks
Nov 22 '05 #1
5 1991
GDL <GD*@discussions.microsoft.com> wrote:
I'm using a FileStream as below:

FileStream fs = new FileStream
(
filePath,
FileMode.Create,
FileAccess.Write,
FileShare.None
);

//do some writing
fs.Write(...)

//close
fs.Close();

My problem is that even after the stream is closed no other applications can
access the file (resource in use) until I close the application which created
it.

Can anybody help with this please?


Given the code above, it's possible for an exception to occur and the
file handle not to be closed. A using statement would sort this:

using (FileStream fs = new FileStream (...))
{
// Do the writing
fs.Write (...);

// No need to manually close
}

If exceptions aren't occurring, that shouldn't be the problem, but the
above is better practice anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
GDL
Hi Jon,

The code snippet is actually wrapped in try / catch blocks in the
application and I'm happy that exceptions are not occuring, also the file
written by the statement is correctly formatted.

I will restructure as per your suggestion for better practice but do you
have any other thoughts as to why the file is remaining locked? I have
similar code in various places throught the application and they all exhibit
the same problem.
--

"GDL" wrote:
Hi,

I'm using a FileStream as below:

FileStream fs = new FileStream
(
filePath,
FileMode.Create,
FileAccess.Write,
FileShare.None
);

//do some writing
fs.Write(...)

//close
fs.Close();

My problem is that even after the stream is closed no other applications can
access the file (resource in use) until I close the application which created
it.

Can anybody help with this please?

--
Many Thanks

Nov 22 '05 #3
GDL <GD*@discussions.microsoft.com> wrote:
The code snippet is actually wrapped in try / catch blocks in the
application and I'm happy that exceptions are not occuring, also the file
written by the statement is correctly formatted.

I will restructure as per your suggestion for better practice but do you
have any other thoughts as to why the file is remaining locked? I have
similar code in various places throught the application and they all exhibit
the same problem.


Nothing else immediately comes to mind.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4
GDL
Jon,

I've done some more investigation and the problem seems to be time related -
if I wait for what seems to be a variable amount of time but typically
several 10's of minutes then the locks are released - does this point to
garbage collection issues relating to the underlying file handles?

--

"Jon Skeet [C# MVP]" wrote:
GDL <GD*@discussions.microsoft.com> wrote:
The code snippet is actually wrapped in try / catch blocks in the
application and I'm happy that exceptions are not occuring, also the file
written by the statement is correctly formatted.

I will restructure as per your suggestion for better practice but do you
have any other thoughts as to why the file is remaining locked? I have
similar code in various places throught the application and they all exhibit
the same problem.


Nothing else immediately comes to mind.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #5
GDL <GD*@discussions.microsoft.com> wrote:
I've done some more investigation and the problem seems to be time related -
if I wait for what seems to be a variable amount of time but typically
several 10's of minutes then the locks are released - does this point to
garbage collection issues relating to the underlying file handles?


Yes - it suggests that somewhere, you're opening a FileStream and not
closing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6

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

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
2
by: Petr Vostry | last post by:
Hello, I have problem with locking of resource files in my ASP.NET application running on Windows 2003 Server. This resource file I can't delete, only after restart and/or waiting about 15...
5
by: GDL | last post by:
Hi, I'm using a FileStream as below: FileStream fs = new FileStream ( filePath, FileMode.Create, FileAccess.Write, FileShare.None
9
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was...
2
by: Gidi Morris via .NET 247 | last post by:
Hi, Is there a way to play audio using MCI (I preffer MCI, but if you know of any others I'll be greatful) from a Filestream? I mean, I have a filestream to an audio file, can I play the audio?...
3
by: Grandpa Pete | last post by:
How can I share resources across all users of a web service for read/write access. *Situation* *one*: I want to have an in memory counter that all users of the webservice could access. Call...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
2
by: sham | last post by:
Hi to all, I have a file that when I try to open gives the exception message "Access to path c:\data\abc.mpg is denied". As soon as I try to do file = new FileStream(filename, FileMode.Open);...
0
by: Timothy Grant | last post by:
On Mon, Aug 18, 2008 at 10:58 PM, tarun <tarundevnani@gmail.comwrote: Does making it a class level instead of instance level variable not work? -- Stand Fast, tjg.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.