如何获取特定用户的 Windows“特殊文件夹”的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/198124/
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
How can I get the path of a Windows "special folder" for a specific user?
提问by kgriffs
Inside a service, what is the best way to determine a special folder path (e.g., "My Documents") for a specific user? SHGetFolderPath allows you to pass in a token, so I am assuming there is some way to impersonate the user who's folder you are interested in.
在服务内部,确定特定用户的特殊文件夹路径(例如,“我的文档”)的最佳方法是什么?SHGetFolderPath 允许您传入令牌,因此我假设有某种方法可以模拟您感兴趣的文件夹的用户。
Is there a way to do this based just on a username? If not, what is the minimum amount of information you need for the user account? I would rather not have to require the user's password.
有没有办法仅基于用户名来做到这一点?如果没有,您需要为用户帐户提供的最少信息量是多少?我宁愿不必要求用户的密码。
(Here is a related question.)
(这是一个相关的问题。)
采纳答案by Nick
I would mount the user's registry hive and look for the path value. Yes, it's a sub-optimal solution, for all the reasons mentioned (poor forwards compatibility, etc.). However, like many other things in Windows, MS didn't provide an API way to do what you want to do, so it's the best option available.
我会挂载用户的注册表配置单元并查找路径值。是的,由于提到的所有原因(前向兼容性差等),这是一个次优的解决方案。然而,就像 Windows 中的许多其他东西一样,MS 没有提供 API 方式来做你想做的事情,所以它是最好的选择。
You can get the SID (not GUID) of the user by using LookupAccountName. You can load the user's registry hive using LoadUserProfile, but unfortunately this also requires a user token, which is going to require their password. Fortunately, you can manually load the hive using RegLoadKeyinto an arbitrary location, read the data, and unload it (I think).
您可以使用LookupAccountName获取用户的 SID(而非 GUID)。您可以使用LoadUserProfile加载用户的注册表配置单元,但不幸的是,这也需要用户令牌,这将需要他们的密码。幸运的是,您可以使用RegLoadKey手动将配置单元加载到任意位置,读取数据并卸载它(我认为)。
Yes, it's a pain, and yes, it's probably going to break in future versions of Windows. Perhaps by that time MS will have provided an API to do it, back-ported it into older versions of Windows, and distributed it automatically through Windows update... but I wouldn't hold my breath.
是的,这很痛苦,是的,它可能会在未来版本的 Windows 中崩溃。也许到那个时候 MS 会提供一个 API 来做这件事,将它反向移植到旧版本的 Windows 中,并通过 Windows 更新自动分发它......但我不会屏住呼吸。
P.S. This information intended to augment the information provided in your related question, including disclaimers.
PS 此信息旨在补充您的相关问题中提供的信息,包括免责声明。
回答by Frederik Slijkerman
Please, do not go into the registry to find this information. That location might change in future versions of Windows. Use SHGetFolderPath instead.
请不要进入注册表查找此信息。在未来版本的 Windows 中,该位置可能会更改。请改用 SHGetFolderPath。
http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
Edit: It looks like LogonUserwill provide the token for the other user that you need.
编辑:看起来LogonUser将为您需要的其他用户提供令牌。
回答by Andy
You might try calling ImpersonateLoggedOnUser()to modify a user token for another user, and then passing that to SHGetFolderPath(). Based on the doc for ImpersonateLoggedOnUser(), it looks like you can call LogonUser() to get a token for a specific user.
您可以尝试调用ImpersonateLoggedOnUser()来修改另一个用户的用户令牌,然后将其传递给 SHGetFolderPath()。根据 ImpersonateLoggedOnUser() 的文档,您似乎可以调用 LogonUser() 来获取特定用户的令牌。
Just from reading around, I'd guess that the user in question must be logged on in some form in order for this to work. I recall one page stating that the user's registry hive must be mounted in order for this to work (which makes some sense I suppose).
只是通过阅读,我猜有问题的用户必须以某种形式登录才能使其工作。我记得有一页指出必须安装用户的注册表配置单元才能使其正常工作(我认为这有一定道理)。
回答by m_pGladiator
This information is stored in the registry in the key "HKEY_USERS\S-1-5-21-616815238-485949776-2992451252-3228\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders".
此信息存储在注册表中的注册表项“HKEY_USERS\S-1-5-21-616815238-485949776-2992451252-3228\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders”中。
The "S-1-5-21-616815238-485949776-2992451252-3218" is the GUID of the user. You need to get this GUID to find the corresponding key and read it.
“S-1-5-21-616815238-485949776-2992451252-3218”是用户的GUID。您需要获取此 GUID 才能找到相应的密钥并读取它。
In thisexample they use SHGetFolderPath function you mention and there is a list with all special folders which might be helpful.
在这个例子中,他们使用你提到的 SHGetFolderPath 函数,并且有一个包含所有可能有用的特殊文件夹的列表。
NOTE:Microsoft discourages to use the registry key, since it is still there just for backward compatibility
注意:Microsoft 不鼓励使用注册表项,因为它仍然存在只是为了向后兼容