开发者

File.WriteAllText Weirdness In Windows 7?

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:\U开发者_如何学运维sers\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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜