Question

I am using C# with .net 2.0

I am saving my program data in a file under: C:\ProgramData\MyProgramName\fileName.xml

After installing and running my application one time I uninstalled it (during uninstallation I'm removing all the files from "program data") and then I reinstall the application, and ran it.

The strange thing is that my application started as if the files in program data existed - means, I had old data in my app even though the data file was deleted.

When running:

File.Exists("C:\ProgramData\MyProgramName\fileName.xml")

I got "true" even though I knew for sure that the file does not exist.

The thing became stranger when I ran the application as admin and then the file didn't exist.

After a research, I found out that when running my application with no admin privileges instead of getting:

C:\ProgramData\MyProgramName\fileName.xml

I get

C:\Users\userName\AppData\Local\VirtualStore\ProgramData\MyProgramName\fileName.xml

and indeed there was a file that existed from the previous installation (that I obviously didn't delete, because I didn't know it existed).


So just guide me how could I stop this when apps running with no admin right.

I do not want to create any file automatically in VirtualStore folder. Please discuss all the possible ways to stop this.

Was it helpful?

Solution

First, ask yourself, do this need to be globally saved for all users?

If it doesn't have to be, save the file in Application Data instead, you can get the path with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), it should always reliably expand to C:\Users\Username\AppData\Roaming\. Do note that this path is unique for each user though.

If you have to, you're out of luck. There is no reliable way to store application data for all users without admin rights (or UAC) on any Windows post-XP that's not extremely hacky, like storing your data in the Public user (which may or may not be possible, I can't check right now).

OTHER TIPS

An approach to solving this is to use the Environment.SpecialFolder.CommonApplicationData location, but with some very important caveats & setup.

CommonApplicationData is

The directory that serves as a common repository for application-specific data that is used by all users.

This location is described further here and here.

Important requirements and restrictions are given in another SO answer: https://stackoverflow.com/a/22107884/3195477

which said in part:

The recommended solution is for your installer to create a sub directory of C:\ProgramData for your shared storage. And that sub directory must be given a permissive ACL by the installation program. That is what grants the desired access to all standard users.

Otherwise the program running with standard user permission will still not be all equally able to read/write files in that location for all users.

I found a work around for this issue when transferring a very old win32 app to windows 7 & 10. The program wrote to a database on C:\Program Files... but the OS auto changed the path to virtual store. However the database was required globally. By changing compatablilty mode to Windows 95 or XP SP2 and always running as administrator the database was worked on directly in C:\Program Files\etc.

There are security implications for this and the box was removed from all networks and adapters disabled etc.

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