Take the 2-minute tour ×

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.


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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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)


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...

share|improve this answer
Yes. I have to.... it should be shaerd between all users. And I am using Environment.SpecialFolder.ApplicationData to get the correct path –  user844541 Apr 3 '12 at 15:55
@user844541 -- check my edit. I wonder if it has something to do with this. –  AKX Apr 3 '12 at 16:07
first of all - thanks! The truth is that I have more than one file under program data , but that is the only file that being redirected. Regarding your guess - it sounds reasonable, you pointed out a good reason for that to happen .. I'll do some more research.. thanks a lot/ –  user844541 Apr 3 '12 at 18:30

I found the reason for the bug.

the application is trying to take ownership on the file and then the other file is created.

I removed that line and now everything works just fine.

share|improve this answer

Your Answer


