How to get (programatically) the location of a Windows UAC virtualized file?

喜你入骨 提交于 2019-12-11 01:39:13

问题


I have a 32-bit windows old application (with the C/Win32 source) that creates its data file in the same folder where the executable is.

Because the application has no installation program and the user can place the executable wherever he/she wants, the application has a dialog to inform the user where its data is located.

But under Microsoft Vista/Seven if the user puts the application in the Program Files or any other system-protected folder the data file gets virtualized and moved to a virtual-store.

If, under Vista/Seven, I still want to inform the user where the data file is located: (without preventing virtualization in the manifest file)

  1. How can I know (programatically) if the data file is virtualized? Or if the folder where the executable is located implies that data file will be virtualized?

  2. Assuming I know the data file is virtualized, how can I know (programatically) the location of the virtual folder, to show it in the information dialog?

I have found the following question very close to what I'm asking but it doesn't show the solution (if any) of knowing when virtualization is taking place for a file, and where it gets virtualized.

How to detect file redirection to the Windows VirtualStore?


回答1:


Virtualization is transparent to the app. The only way to know whether it is being virtualized is to ask the OS, per the answer in the question you linked to (use GetTokenInformation() with the TokenVirtualizationEnabled flag), but there is no way (that I know of) of asking the OS where the virtualized items are actually stored, as it may be different from one OS version to the next. You will have to do some research and then hard-code the paths for each given OS that your app detects at runtime.

If you are going to update your code to detect virtualization, then you are better off updating the code to play nicer with UAC instead. Stop storing your files where they do not belong, and start storing them where Microsoft wants you to store them. In this case, within the user's profile instead. Use SHGetFolderPath() or related function to locate the user's CSIDL_LOCAL_APPDATA folder (or SHGetKnownFolderPath() on Vista+ to locate the FOLDERID_LocalAppData folder), then create a subfolder underneath it for your app to store its data files in.



来源:https://stackoverflow.com/questions/17953426/how-to-get-programatically-the-location-of-a-windows-uac-virtualized-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!