473,651 Members | 2,466 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 2003
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
14228
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
1346
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
5846
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
2087
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
2797
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
3679
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
7672
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
5612
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
1064
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
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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
7296
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...
1
6157
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
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
4143
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
2696
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
1585
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.