文件从程序数据重定向到AppData \ Local \ VirtualStore \ ProgramData [英] File redirection from Program data to AppData\Local\VirtualStore\ProgramData

查看:614
本文介绍了文件从程序数据重定向到AppData \ Local \ VirtualStore \ ProgramData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#与.net 3.5配合使用

I am using C# with .net 3.5

我将程序数据保存在以下文件中:C:\ Program Data \ MyProgramName \ fileName.xml

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.

运行时:

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

即使我确定该文件不存在,我也得到了"true".

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.

经过研究,我发现在没有管理员特权的情况下运行应用程序而不是获取:"C:\ Program Data \ MyProgramName \ fileName.xml",我得到的是"C:\ Users \ userName \ AppData \ Local \ VirtualStore \ ProgramData \ MyProgramName \ fileName.xml"

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.

我的问题是:

  1. 为什么会这样.
  2. 如何防止它发生

预先感谢

推荐答案

实际上 是否必须写入每个系统的Program Data文件夹,而不是每个用户的Application Data文件夹" ?

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

您可能想看看 Environment.GetFolderPath 和以下 Environment.SpecialFolder s:

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

  • Environment.SpecialFolder.ApplicationData-应用程序数据的数据文件夹,如果用户配置文件正在漫游,则同步到域控制器上
  • Environment.SpecialFolder.LocalApplicationData-用于本地和未同步的应用程序数据的数据文件夹(例如,用于缓存)
  • 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)

在Windows 7 x64(非管理员用户)上进行了测试.

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.");

这符合预期,即.写入C:\ProgramData\MyApp\Test.txt.据我所知(管理员模式命令提示符),也没有UAC虚拟化.

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.

双重

我猜发生了什么事,就是某个管理员用户已将文件写入到ProgramData文件夹中,因此,UAC文件系统虚拟化启动,并将非管理员写入的重定向到VirtualStore.

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.

您的卸载程序是否以管理员身份运行?如果是这样,则可能必须检查启动卸载的用户的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...

这篇关于文件从程序数据重定向到AppData \ Local \ VirtualStore \ ProgramData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆