Jon Box's Weblog

 


.NET Articles by the Atomic group

MSDN RDs









Subscribe to "Jon Box's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 

 

  Monday, November 01, 2004


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).

 


3:14:31 PM    comment []
trackback []


Click here to visit the Radio UserLand website. © Copyright 2004 Jon Box.
Last update: 12/11/2004; 11:13:06 PM.

November 2004
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Oct   Dec