如何获取Windows“特殊文件夹”的路径?为特定用户? [英] How can I get the path of a Windows "special folder" for a specific user?

查看:83
本文介绍了如何获取Windows“特殊文件夹”的路径?为特定用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务中,确定特定用户的特殊文件夹路径(例如我的文档)的最佳方法是什么? 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 who's 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.

(这里是相关问题。)

推荐答案

用户的注册表配置单元并查找路径值。是的,这是一个次优的解决方案,由于所有的原因(差向前兼容性等)。但是,像许多其他的事情在Windows,MS没有提供一个API的方式来做你想做的,所以它是最好的选择。

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.

用户的SID(而不是GUID),方法是使用 LookupAccountName 。您可以使用 LoadUserProfile 加载用户的注册表配置单元,但不幸的是,这还需要一个用户令牌,它需要他们的密码。幸运的是,您可以使用 RegLoadKey 手动加载配置单元到任意位置,读取数据,并卸载它(我想)。

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

是的,这是一个痛苦,是的,它可能会打破在未来的版本视窗。也许到那时,MS将提供一个API来完成它,将它移植到旧版本的Windows,并通过Windows更新自动分发它...但我不能保持呼吸。

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.

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

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

这篇关于如何获取Windows“特殊文件夹”的路径?为特定用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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