Windows:如何为特定机器上的每个用户找到 LocalAppData 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/681572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows: How can I find the LocalAppData directory for each user on a particular machine?
提问by Anodyne
First, some background:
首先介绍一下背景:
Our product needs to integrate with the Lotus Notes client by adding or updating a line in the NOTES.INI file.
我们的产品需要通过在 NOTES.INI 文件中添加或更新一行来与 Lotus Notes 客户端集成。
We don't have a problem if we're dealing with a single-user installation of Notes (i.e. if there are multiple Windows users on the machine, they'll all use the same Notes configuration). In this case, there's a single NOTES.INI file in the Notes installation directory.
如果我们处理 Notes 的单用户安装(即,如果机器上有多个 Windows 用户,他们都将使用相同的 Notes 配置),我们就没有问题。在这种情况下,Notes 安装目录中有一个 NOTES.INI 文件。
However, under a multi-user installation of Notes (where each Windows user has their own Notes configuration), each user has their own NOTES.INI file stored in the user's LocalAppData directory - e.g. C:\Documents and Settings\Username\Local Settings\Application Data\Lotus\Notes.
但是,在 Notes 的多用户安装(其中每个 Windows 用户都有自己的 Notes 配置)下,每个用户都有自己的 NOTES.INI 文件存储在用户的 LocalAppData 目录中 - 例如 C:\Documents and Settings\Username\Local Settings \Application Data\Lotus\Notes。
So here's the problem: If our product is being installed on a machine with a multi-user installation of the Notes client, we need to be able to update the NOTES.INI file in the profile of each user on that machine.
所以问题来了:如果我们的产品安装在多用户安装 Notes 客户端的机器上,我们需要能够更新该机器上每个用户的配置文件中的 NOTES.INI 文件。
We can do this by having a program run when users log in, which checks whether that user's NOTES.INI file has been updated yet and if not, updates it. However, the uninstall process for our application needs to be able to reverse these modifications for all users on the machine.
我们可以通过在用户登录时运行一个程序来实现这一点,该程序检查该用户的 NOTES.INI 文件是否已更新,如果没有,则更新它。但是,我们应用程序的卸载过程需要能够为机器上的所有用户撤销这些修改。
Hence the question: assuming our code is running with local admin rights, is there some way that we can iterate through each user's profile and find their LocalAppData directory so we can make the necessary changes?
因此问题是:假设我们的代码以本地管理员权限运行,是否有某种方法可以遍历每个用户的配置文件并找到他们的 LocalAppData 目录,以便我们进行必要的更改?
Any suggestions greatly appreciated :-)
非常感谢任何建议:-)
EDIT 2009-03-25 16:52 GMT:
Looks like I have a possible approach (thanks Martin C):
编辑 2009-03-25 16:52 GMT:
看起来我有一个可能的方法(感谢 Martin C):
For each subkey of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList: If it's a "real" user (determined somehow): Remember the subkey name - that's the user's SID Read the ProfileImagePath value If the user's Registry hive is not already loaded (i.e. there is no subkey of HKEY_USERS with the appropriate SID): Enable the SE_BACKUP_NAME and SE_RESTORE_NAME privileges Load the hive from ProfileImagePath\NtUser.dat using RegLoadKey Try to find the user's LocalAppData folder using each of the following Registry keys in turn: HKEY_USERS\<SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders HKEY_USERS\<SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders Expand environment variables in the resulting path if necessary (presumably just expanding %USERPROFILE% to the ProfileImagePath we got earlier) Use the path to find the user's NOTES.INI file and make the appropriate changes If we had to load the hive: Unload the hive using RegUnLoadKey
I can probably get that coded up, but it seems a tiny bit fragile and there's potentially a number of ways it can go wrong. Anyone have a more "official" approach?
我可能可以将其编码,但它似乎有点脆弱,并且可能有多种方式出错。有人有更“官方”的方法吗?
回答by Martin C.
You can enumerate the sub-keys of
您可以枚举的子键
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
Each subkey contains the "ProfileImagePath", which will point to the base-path of the profile. Dependent on the OS-version and the language setting you can then determine the location of LocalAppData (beware, it is language-dependent!).
每个子项都包含“ProfileImagePath”,它将指向配置文件的基本路径。根据操作系统版本和语言设置,您可以确定 LocalAppData 的位置(注意,它取决于语言!)。
Edit:A possible starting point for going further could be
编辑:走得更远的一个可能的起点可能是
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
Unfortunately this could vary from user to user and HKEY_USERS only contains the keys of users which have loaded profiles. You could try if you could load profiles somehow (maybe one can attach a user's registry somehow if it is not already loaded to HKEY_USERS?).
不幸的是,这可能因用户而异,并且 HKEY_USERS 仅包含已加载配置文件的用户的密钥。您可以尝试以某种方式加载配置文件(如果尚未加载到 HKEY_USERS,也许可以以某种方式附加用户的注册表?)。