Question

In one of my projects I am using a path in the System.Environment.SpecialFolder.CommonApplicationData folder (in my case C:\ProgramData) to store configuration and registration data. However I ran into problems. I check if I can access the files by using the routine

Dim isok As Boolean = True
Try
   Dim ftry As New IO.FileStream(frmMain.APPDATA & "\regid.bin", FileMode.Open)
   ftry.Close()
Catch ex As Exception
  isok = False
End Try

APPDATA is the path to my config folder and I check if the file actually exists before that as well (it does). On one machine this routine returns false for me. The exception says no file access. If I rewrite this routine:

Dim isok As Boolean = True
Try
   Dim contents() as Byte = IO.File.ReadAllBytes(frmMain.APPDATA & "\regid.bin")
Catch ex As Exception
  isok = False
End Try

It returns true, so the IO routines obviously can access the file.

Can someone explain to me what the difference between the two methods are? Shouldn't file access be determined by the application's privileges? Is there a better way to determine if I have read/write access to the file?

Was it helpful?

Solution

The FileStream constructor passes off to another constructor overload.. namely this one:

internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost)

The FileAccess it passes by default is FileAccess.ReadWrite. If your application does not have write permissions.. chances are this will fail.

On the other hand, File.ReadAllBytes does this:

using (var fileStream = new FileStream(..., ..., FileAccess.Read, ...
//                                                      ^^^^ this

Notice File.ReadAllBytes uses the FileAccess.Read value.

So that's the difference. FileStream.ctor uses FileAccess.ReadWrite, File.ReadAllBytes uses FileAccess.Read.

TLDR: Your app needs write permissions to use that basic constructor for FileStream. Remember there is a File.Exists method that is meant for this exact purpose. Opening and closing a file to see if it exists is not the right way to go about it.

OTHER TIPS

use IO.File.Exist() routine to check if the file exist. You don't need to read the file content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top