Strange error opening package to write

First off, I'm using .NET 4.

I am trying to write some files to a package, and I come across something strange when I do this:

using (var package = Package.Open(filename, FileMode.OpenOrCreate, FileAccess.Write)) { // do something with package } 

Package refers to System.IO.Packaging.Package .

It is strange that the Package.Open method throws an exception saying:

It is not possible to get a stream using FileMode.Create, FileMode.CreateNew, FileMode.Truncate, FileMode.Append when access is FileAccess.Read.

I found an old bug report from 2009 on Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/details/392318/argumentexception-text-is-wrong

But that does not help.

So, did anyone have an idea?

+7
source share
1 answer

I think I found the problem.

When I changed the code to do this:

 using (var stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) { using (var package = Package.Open(stream)) { // do something with package } } 

I got a pretty decent error message:

The package cannot be opened because the FileMode or FileAccess value is not valid for the stream.

I think there is a β€œreal” error message, and that somewhere someone just mixed it up with this pointless when doing localization.

Then I changed the code for this:

 // no FileAccess parameter using (var package = Package.Open(file, FileMode.OpenOrCreate)) { // do something with package } 

And it won't work anymore and seems to be working correctly.

+6
source

All Articles