Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 249
Sugrue Avatar asked Oct 21 '16 08:10

Sugrue


1 Answers

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.

like image 95
Nikita Avatar answered Oct 12 '22 06:10

Nikita