The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Oh, That Parameter in the FileStream Constructor!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jon Box

Posts: 244
Nickname: jonbox
Registered: Dec, 2003

Jon Box is a .NET Developer and Regional Director.
Oh, That Parameter in the FileStream Constructor! Posted: Nov 1, 2004 1:50 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jon Box.
Original Post: Oh, That Parameter in the FileStream Constructor!
Feed Title: Jon Box's Weblog
Feed URL: http://radio-weblogs.com/0126569/rss.xml
Feed Description: This is a log of my findings and amusements with .NET. I also present information on my presentations and others that I see.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jon Box
Latest Posts From Jon Box's Weblog

Advertisement

OK, if you think Jon Box is smart, please do not read the rest of this.

 

Today, I had a Windows Service started throwing an error on the opening of a text file after months of working nicely.  After some investigation, I found a couple of issues that brought this to be.  First, another vendor’s application which creates the files installed an upgrade today and this new version keeps the file open all of the time (well, for today anyway, which is when I want to read it).  That’s cool – we should be able to handle that.  Here is my code that was opening the file in Read only mode:
fs = New FileStream("\\yada\yada.txt", FileMode.Open, FileAccess.Read)
What’s wrong with it?  Why would it throw an error for a file that is reportedly not locked by the other system?

So, after looking at the FileStream constructor documentation, there is a fourth parameter called share that is of type FileShare.  This is an enumeration that contains constants for controlling the kind of access other FileStreams can have to the same file.  The keyword here is “other”.  This is obviously an important thing to communicate to the file system but I somehow assumed this was the default activity with the FileAccess set to Read.  Now, it looks like:

fs = New FileStream("\\yada\yada.txt", FileMode.Open, _
          FileAccess.Read, FileShare.ReadWrite)

Problem solved (after a little embarrassment).

 

Read: Oh, That Parameter in the FileStream Constructor!

Topic: Forum opened in Developers.ie Previous Topic   Next Topic Topic: Web services contract-first: Thoughts - Part 1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use