且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-29 15:29:58

用户的注册表配置单元并查找路径值。是的,这是一个次优的解决方案,由于所有的原因(差向前兼容性等)。但是,像许多其他的事情在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.