Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File redirection from Program data to AppData\Local\VirtualStore\ProgramData

I am using C# with .net 3.5

I am saving my program data in a file under: C:\Program Data\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 thig 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:\Program Data\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 priviliges instead of getting: "C:\Program Data\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 apparentlly there is some virtual path to the file under program data.

EDIT :

I found out that after deleting the old file in the virtual store, my application is suddenly able to find the correct file. (I didn't make any changes in the file under Program Data.

My question is:

  1. why is it happen.
  2. How can I prevent it from happening

Thanks in advance

like image 390
user844541 Avatar asked Feb 20 '23 22:02

user844541


1 Answers

Do you actually have to write to the per-system Program Data folder instead of the per-user Application Data folder(s)?

You might want to take a look at Environment.GetFolderPath and the following Environment.SpecialFolders:

  • Environment.SpecialFolder.ApplicationData - data folder for application data, synchronized onto domain controller if the user profile is roaming
  • Environment.SpecialFolder.LocalApplicationData - data folder for application data, local and not synchronized (useful for, for instance, caches)

EDIT:

Tested on Windows 7 x64, non-administrator user.

var appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var myFolder = Path.Combine(appData, "MyApp");
if(!Directory.Exists(myFolder)) Directory.CreateDirectory(myFolder);
File.WriteAllText(Path.Combine(myFolder, "Test.txt"), "Test.");

This does what is expected, ie. writes into C:\ProgramData\MyApp\Test.txt. As far as I can tell (Administrator mode Command Prompt), there's no UAC virtualization going on either.

Double edit:

I guess what's happened is that at some point an Administrator user has written the files into your ProgramData folder, and as such, UAC file system virtualization kicks in and redirects the non-administrator writes into the VirtualStore.

Does your uninstaller run as Administrator? If it does, you might have to check both the VirtualStore path for the user who initiates the uninstall, and the actual file system path for program data to remove. I'm not sure if there's an official way to do this, though...

like image 113
AKX Avatar answered Apr 09 '23 12:04

AKX