Sugrue Sugrue - 1 month ago 7
C# Question

How to skip the VirtualStore and read files in Program Files folder?

I have a c# application on Windows 10 PC. There are settings files in the install folder (C:\Program Files (x86)\xxx) which I want to read, but not edit unless user has admin access. The problem is that windows is copying these settings files to the VirtualStore and redirecting all reads there - whereas the same app run as admin sees the original settings files in the Program Files folder.

My question: Is there a way to make the application see the original files in the Program Files even when not run as admin? I just want to read them, not edit them.

Answer

You don't need elevated permissions to read the file from Program Files (x86) folder. Check how you open the file for reading. You should specify different FileAccess flag in common user mode and in elevated mode. For common user mode it should be opened with 'FileAccess.Read`:

using (FileStream settingsFile = new FileStream(@"C:\Program Files (x86)\xxx", FileMode.Open, FileAccess.Read))
{
  // Do the job
}

To detect if application runs with elevated permissions use IsProcessElevated method. Depending on the result you are able to select proper FileAccess mode.