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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With