Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Opening same file for read multiple times?


  • Please log in to reply
4 replies to this topic

#1 Cabadam

Cabadam

    Advanced Member

  • Members
  • PipPipPip
  • 90 posts

Posted 19 February 2012 - 05:13 PM

Busy weekend!

Does the ND+ and/or .NET Micro Framework support the FileShare enumeration when opening FileStreams?

Reason I ask, is this small application fails:
            ArrayList streams = new ArrayList();
            string file = @"\SD\schedules\6337ddf1-b652-68ea-d1b8-576d138ba855";
            streams.Add(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read));
            streams.Add(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read));

Replace the file variable with the path to some file on your SD card.

It is explicitly only opening read access and explicitly allowing FileShare.Read, but the second filestream throws an IO exception.

(ArrayList stuff isn't directly relevant, I just wanted to ensure the garbage collector didn't gobble up the first stream before the second one ran).

Thanks!

#2 telno

telno

    New Member

  • Members
  • Pip
  • 8 posts

Posted 03 April 2013 - 08:20 PM

I'm experiencing the same problem. I get an IO exception on the second FileStream.  In my case, the first stream is a writer (log writer), and the second is a reader (log reader).

 

 

 

 

string fileName = @"SDtest.log";var writeStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);// this line will fail with IOException (ErrorCode=UnauthorizedAccess)var readStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); 

 

 
 


#3 telno

telno

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 April 2013 - 03:48 PM

I got this working after looking at the Netduino source for: NetduinoGoFirmware / Framework / Core / Native_IO / FileSystemManager.cs

 

The problem seems to be this section:

 

if ((share != FileShareReadWrite) ||((current.Share & access) != access)){throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess);}

 

First of all, this code means that [color=#ff0000;]only ReadWrite shared access is supported[/color].  This isn't documented anywhere I could find.

 

Second, the code creates a dependency on those enumeration values being the same (and never changing).  Bad, coupling.

 

 

To work around the issue, update the file access and share permissions to ReadWrite:

 

string fileName = @"SDtest.log";var writeStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);var readStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);


#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 04 April 2013 - 04:23 PM

I got this working after looking at the Netduino source for: NetduinoGoFirmware / Framework / Core / Native_IO / FileSystemManager.cs

 

I just wanted to clarify this is not official source code repository, you should be using .NET Micro Framework on Codeplex and the official Secret Labs firmware source, which gets [usually] published on the forum. Luckily the source code of core framework libraries in NetduinoGoFirmware repository is not modified, but it should not be used as authoritative reference. Thank you for your understanding.



#5 telno

telno

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 April 2013 - 11:49 PM

Good point.  I just found this via Google, and when the code fix worked successfully I knew it was still relevant to the official source.

 

 

Would love to hear from someone about a fix for this issue.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.