tags:

views:

104

answers:

2

This one had me going crazy for awhile.

I have an app that was behaving strangely and claiming files had been written when they clearly had not. It was especially confusing because when I ran this app in debug mode or the bin directory of my project it seemed to work fine. As soon as I moved it to a folder within the Program Files folder weird things started happening.

It took me awhile to track it down but for some reason when I tried to write to:
C:\Program Files\<some path>\test.txt
Using File.WriteAllText it would instead be written to:
C:\Users\spencer\AppData\Local\VirtualStore\Program Files\<some path>\test.txt

What further confused me is that File.Exists("C:\Program Files\<some path>\test.txt") was returning true.

This doesn't seem to happen across the board however. Another portion of my app uses the File.OpenWrite() method which would write the file to the actual path I specified and does not toss the file into the virtual store.

EDIT: It would seem that StreamWriter ignores the specified path and tosses files in the virtual store as well.

Is this behavior documented anywhere?

+1  A: 

This is due to Windows Vista's (and Window 7's) File Virtualization feature.

Scott Hanselman blogged about this a while back, and provides a great discussion of why it exists.

Basically, programs should never write to Program Files. Windows Vista and Windows 7 automatically "fix" old, poorly behaving programs, by redirecting this write to the user's local data folder.

Reed Copsey
Well they forgot to "fix" `File.OpenWrite()`. Also where am I supposed to save data that applies to all users?
Spencer Ruport
Spencer: You're really not supposed to do that. Part of the "developing for Vista" is that user data should be isolated. Saving data shared by all users requires elevated permissions, and should probably be done during the install of the software (since you'll have admin rights at that point in time).
Reed Copsey
+ I'm not sure his article is entirely correct. I do have write permissions to the folder.
Spencer Ruport
Well I'm developing a Windows Service. Does File Virtualization apply to Service accounts? If so where do those files reside and if not where should I be putting Service account data?
Spencer Ruport
No. Service accounts should have permissions to write. If you're doing this, and getting it sent where you displayed, you're obviously running this as a standard user, though...
Reed Copsey
Well I was running into some file write issues when running under a service account so I eventually tried running the program directly. Perhaps I'm dealing with two separate issues. Thanks a lot for your insight. :) +1
Spencer Ruport
A: 

Very interesting.. Maybe win7 doesn't allow to write to program files dir at all and you have to use IsolatedStorage...

Gopher