473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1994
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
14169
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
1337
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
358
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
5836
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
2081
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
2783
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
3660
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
7661
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
5601
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
1057
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
7199
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
7076
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
7274
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,...
1
6984
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
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.