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

I have a C # application on a Windows 10 PC. In the installation folder there are settings files (C: \ Program Files (x86) \ xxx) that I want to read but not edit if the user does not have administrator access. The problem is that windows copy these settings files to VirtualStore and redirect everything they read there, while the same application starts as admin and sees the source settings files in the Program Files folder.

My question is: is there a way to make the application see the source files in Program Files even if it is not running as admin? I just want to read them, not edit them.

+8
c # windows-10 virtualstore
source share
2 answers

You do not need elevated permissions to read a file from Program Files (x86) . Check how you open the file for reading. You must specify a different FileAccess flag in normal user mode and in elevated mode. For normal user mode, it should be opened using "FileAccess.Read":

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

Use the IsProcessElevated method to determine if the application is running at elevated permissions. Depending on the result, you can choose the correct FileAccess mode.

+7
source share

The way to view the source files in Program Files is not to write there (and to do this, open files for reading only, as Nikita points out). VirtualStore is not used, but to fix application problems. Such problems are caused, for example, by broken applications written for old single-user Windows, when the current day of Windows (with NT) can have several sessions at the same time from different users.

If an application wants to modify data files common to all users, it must save the files in the All Users Profile. If this is user data, it can store data in the Application Data folder in the user profile. In the application data, you are still left with the option if you want the data to be roaming or local.

The paths to these folders are different in different versions of Windows. Windows Installer has properties set for paths. Applications have many interfaces that they can use. See Working with Famous Folders in Applications and SHGetKnownFolderPath for a single interface.

In addition, access to program files is located behind the UAC . You should read on it to get all the details.

+1
source share

All Articles