tags:

views:

25

answers:

1

Our application requires a user to select a photograph from their camera. The cameras I tested with while adding this feature would all appear as a drive letter in "My Computer" so loading and working with the image was a no-brainer. A coworker gave me his camera which rather than mount as a drive it triggers the awful "Scanner and Camera Wizard". I was encouraged to see that in "My Computer" there was an entry for the camera and indeed I was able to browse to and select files from a standard OpenFileDialog. - GREAT!

Except that when I attempt to ACCESS the file I receive "UnauthorizedAccessException". Upon investigation I see that it's actually loading the files from this location: C:\Documents and Settings\sk\Local Settings\Temporary Internet Files\Content.IE5\AXY0DNE3

What in the world?! IE5.5??

From here things keep going downhill. That location is apparently a very well hidden location that I can only navigate to by directly entering the path in the explorer bar. I then figured I could just copy the file to a temp location and work with it from there. So I did that but I still can't work with the file, throwing the same exception: Access to the path 'C:\Documents and Settings\sk\Local Settings\Temp\IMG_0005[1].jpg' is denied.

It appears that the permission settings were copied along with the file (makes sense).

As a workaround I have instructed my users to use the terrible little wizard, copy the files to a temporary location and then select them fro there. I don't like this but I needed to get this feature deployed Today. With a workaround in place I would now like to try and get this working if possible. Ideally I could just work with the file without copying it around to other locations, etc. What I don't understand is why the UnauthorizedAccessException is being throw.

I'm hoping that someone out there has faced a similar challenge and can share some tips on how to work with these files. I'd rather not go the whole WIA route and work with the files via the camera interface.

A: 

The file is Read Only. I should have checked this!

Of course there are many ways to handle this. At first I thought "I will just force the read only attribute down" but then I thought "wait, I shouldn't need to do that, I am after all only READING the file" So long story short that made me look deep into my IO library and find that I wasn't explicitly setting FileAccess to Read like I should:

public static byte[] ReadWholeFileBytes(string filename)
{
    Guard.ArgumentNotNullOrEmptyString(filename, "filename");

    if(!File.Exists(filename))
    {
        throw new FileNotFoundException("Failed finding file " + filename);
    }

    using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
    {
        return ReadWholeStream(stream);
    }
}

However for the sake of making this answer complete for others you can drop the Read Only with this little bit of code:

File.SetAttributes(openFile.FileName, FileAttributes.Normal);
Steve K