Missing a little FileAccess.Read and it doesn’t work

May 22, 2010

What is the difference between this code

using (var fileStream = new FileStream(settingsFilename, FileMode.Open))
{
    return ReadSettings(fileStream);
}

And this code

using (var fileStream = new FileStream(settingsFilename, FileMode.Open, FileAccess.Read))
{
    return ReadSettings(fileStream);
}

Almost nothing, just a little FileAccess.Read !

But this little thing makes a big difference when you run your software in a secured environment.

The application with this piece of code was deployed to a customer reporting that the application was crashing at a the point of reading the settings. Weird, really weird. After getting back the log and I finally discovered that using juste FileMode.Open needs modify rights and that’s was the issue because the customer deploy the settings file on a folder in which the user doesn’t have the modify rights.