473,804 Members | 3,425 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.Writ e,
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 2018
GDL <GD*@discussion s.microsoft.com > wrote:
I'm using a FileStream as below:

FileStream fs = new FileStream
(
filePath,
FileMode.Create ,
FileAccess.Writ e,
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.co m>
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.Writ e,
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*@discussion s.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.co m>
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*@discussion s.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #5
GDL <GD*@discussion s.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.co m>
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
14264
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, then use the GetThumbnailImage method to create a second Image. This second one is the one I want to get a FileStream from, so that I can then use its Handle to use Response.WriteFile to output the thumbnail on my ASP.Net page. Any help would...
2
1360
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 minutes, both without running of appplication. Thanks for any help. Petr Vostry
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
5860
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 working with the StreamReader object I got a deadlock when I tried reading the file while my external application was writing to it. So far I am able to create a FileStream object and open the file in question (CrawlerBackup.txt). But I am unable to...
2
2093
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? I can't access the file directly, so just using the MCI's MCI_OPEN command won't work. -------------------------------- From: Gidi Morris -----------------------
3
2807
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 it g_count. Then I create a web method called Update_g_count.
3
3693
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 by another process. This occurs even if I release the object using fs.Close() and fs = Nothing. Please help (my code is given below) Best regards
8
7681
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) { if(MyBitmap != null) { Graphics g = e.Graphics;
2
5622
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); the exception is raised. I have rebooted my machine to make sure that nothing is locking the file and yet I always get the same error for this file.
0
1080
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
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
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 we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.