如何获取 Windows“特殊文件夹”的路径 对于特定用户?

发布于 2024-07-07 08:16:20 字数 307 浏览 12 评论 0原文

在服务内部,确定特定用户的特殊文件夹路径(例如“我的文档”)的最佳方法是什么? SHGetFolderPath 允许您传递令牌,因此我假设有某种方法可以模拟您感兴趣的文件夹的用户。

有没有办法仅根据用户名来执行此操作? 如果没有,用户帐户所需的最少信息量是多少? 我宁愿不必要求用户的密码。

(这是一个相关问题。)

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 whose folder you are interested in.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗弃M 2024-07-14 08:16:20

请不要进入注册表查找此信息。 在未来版本的 Windows 中,该位置可能会发生变化。 请改用 SHGetFolderPath。

http://msdn.microsoft.com/en-us/ library/bb762181(VS.85).aspx

编辑:它看起来像 LogonUser 将为您需要的其他用户提供令牌。

Please, do not go into the registry to find this information. That location might change in future versions of Windows. Use SHGetFolderPath instead.

http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx

Edit: It looks like LogonUser will provide the token for the other user that you need.

一个人的夜不怕黑 2024-07-14 08:16:20

我将安装用户的注册表配置单元并查找路径值。 是的,出于所有提到的原因(向前兼容性差等),这是一个次优的解决方案。 然而,与 Windows 中的许多其他功能一样,MS 没有提供 API 方式来完成您想做的事情,因此它是可用的最佳选择。

您可以使用 LookupAccountName。 您可以使用 LoadUserProfile 加载用户的注册表配置单元,但不幸的是,这也需要用户令牌,这将需要他们的密码。 幸运的是,您可以使用 RegLoadKey 到任意位置,读取数据,然后卸载它(我认为)。

是的,这很痛苦,而且它可能会在未来版本的 Windows 中被破坏。 也许到那时微软会提供一个 API 来做到这一点,将其向后移植到旧版本的 Windows 中,并通过 Windows 更新自动分发......但我不会屏住呼吸。

PS 此信息旨在补充您的相关问题中提供的信息,包括免责声明。

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.

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 RegLoadKey into an arbitrary location, read the data, and unload it (I think).

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.

P.S. This information intended to augment the information provided in your related question, including disclaimers.

温柔少女心 2024-07-14 08:16:20

您可以尝试调用 ImpersonateLoggedOnUser() 进行修改另一个用户的用户令牌,然后将其传递给 SHGetFolderPath()。 根据 ImpersonateLoggedOnUser() 的文档,您似乎可以调用 LogonUser() 来获取特定用户的令牌。

通过阅读周围的内容,我猜测相关用户必须以某种形式登录才能正常工作。 我记得有一页指出必须安装用户的注册表配置单元才能使其正常工作(我认为这有一定道理)。

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.

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

枕梦 2024-07-14 08:16:20

此信息存储在注册表中的“HKEY_USERS\S-1-5-21-616815238-485949776-2992451252-3228\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders”项中。

“S-1-5-21-616815238-485949776-2992451252-3218”是用户的 GUID。 你需要得到这个GUID才能找到对应的密钥并读取它。

这个< /a> 例如,他们使用您提到的 SHGetFolderPath 函数,并且有一个包含所有特殊文件夹的列表,这可能会有所帮助。

注意: Microsoft 不鼓励使用注册表项,因为它仍然存在只是为了向后兼容

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

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.

In this example they use SHGetFolderPath function you mention and there is a list with all special folders which might be helpful.

NOTE: Microsoft discourages to use the registry key, since it is still there just for backward compatibility

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文