windows 如何在代码中正确使用 %USERPROFILE%?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6827496/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:36:34  来源:igfitidea点击:

How to properly use %USERPROFILE% inside code?

c++windowswinapivisual-c++environment-variables

提问by karikari

Is my code correct? It seems can compile but does not work properly..

我的代码正确吗?它似乎可以编译但不能正常工作..

CString testing = _T(" --url=") + cstring + _T(" --out=%USERPROFILE%\snapshot.png");

I want to point it to user's folder..but still cannot work.

我想将它指向用户的文件夹..但仍然无法工作。

回答by Cody Gray

The answer is that you don't use environment variables at all. Rather, you use the shell functions specifically designed to retrieve the path of special folders.

答案是您根本不使用环境变量。相反,您可以使用专门设计用于检索特殊文件夹路径的 shell 函数。

On Windows Vista and later, that function is SHGetKnownFolderPath. It takes KNOWNFOLDERIDvaluesto identify the folder whose path you wish to retrieve. In your case, that would be FOLDERID_Profile.

在 Windows Vista 和更高版本上,该函数是SHGetKnownFolderPath. 它需要KNOWNFOLDERID来标识您希望检索其路径的文件夹。在您的情况下,那将是FOLDERID_Profile.

If you need to target earlier versions of Windows (such as XP), you will need to use the SHGetSpecialFolderPathfunction, instead. It takes a CSIDLvalue identifying the folder whose path you wish to retrieve. Again, in your case, that would be CSIDL_PROFILE.

如果您需要针对较早版本的 Windows(例如 XP),则需要改用 SHGetSpecialFolderPath函数。它需要一个CSIDL值来标识您希望检索其路径的文件夹。同样,在您的情况下,那将是CSIDL_PROFILE.


Of course, you should never store data directly in the user's profile folder. So hopefully the bit of code that you've shown is for demonstration purposes only. Applications should only create files in the specific locations underthe user profile folder, designed for application data storage.


当然,您永远不应该将数据直接存储在用户的配置文件文件夹中。所以希望您展示的那段代码仅用于演示目的。应用程序应仅在用户配置文件文件夹的特定位置创建文件,专为应用程序数据存储而设计。

These locations are CSIDL_APPDATAor CSIDL_LOCAL_APPDATA. If you are creating data that the user should be able to modify and should treat as his/her own, then it would be appropriate to store that data in the user's documents folder (CSIDL_MYDOCUMENTS).

这些位置是CSIDL_APPDATACSIDL_LOCAL_APPDATA。如果您正在创建用户应该能够修改并且应该视为他/她自己的数据,那么将该数据存储在用户的文档文件夹 ( CSIDL_MYDOCUMENTS) 中是合适的。

More usage information is available in my answer here.

我的回答中提供了更多使用信息。



Sample code:

示例代码:

TCHAR szFolderPath[MAX_PATH];
if (!SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_APPDATA, FALSE))
{
    // Uh-oh! An error occurred; handle it.
}

Or, using MFC's CStringclass:

或者,使用 MFC 的CString类:

CString buffer;
BOOL bRet = SHGetSpecialFolderPath(NULL, buffer.GetBuffer(MAX_PATH), CSIDL_APPDATA, FALSE);
buffer.ReleaseBuffer();
if (!bRet)
{
    // Uh-oh! An error occurred; handle it.
}

回答by Eddie Paz

As Cody suggested, it's better to use the SHGetSpecialFolderPath function. However, you could use the GetEnvironmentVariable function to get that and other variables set in the system.

正如 Cody 所建议的,最好使用 SHGetSpecialFolderPath 函数。但是,您可以使用 GetEnvironmentVariable 函数来获取系统中设置的该变量和其他变量。

TCHAR szBuf[MAX_PATH] = {0};
::GetEnvironmentVariable(_T( "USERPROFILE" ), szBuf, MAX_PATH);