我的代码正确吗?好像可以编译但是不能正常工作..
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.
答案是您根本不使用环境变量.相反,您可以使用专门设计用于检索特殊文件夹路径的 shell 函数.
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.
在 Windows Vista 及更高版本上,该函数是 SHGetKnownFolderPath代码>.它需要
KNOWNFOLDERID
值 来识别要检索其路径的文件夹.在您的情况下,这将是 FOLDERID_Profile
.
On Windows Vista and later, that function is SHGetKnownFolderPath
. It takes KNOWNFOLDERID
values to identify the folder whose path you wish to retrieve. In your case, that would be FOLDERID_Profile
.
如果您需要针对早期版本的 Windows(例如 XP),您将需要使用 SHGetSpecialFolderPath
函数.它需要一个 CSIDL 值来标识您希望检索其路径的文件夹.同样,在您的情况下,这将是 CSIDL_PROFILE
.
If you need to target earlier versions of Windows (such as XP), you will need to use the SHGetSpecialFolderPath
function, instead. It takes a CSIDL value identifying the folder whose path you wish to retrieve. Again, in your case, that would be 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 under the user profile folder, designed for application data storage.
这些位置是 CSIDL_APPDATA
或 CSIDL_LOCAL_APPDATA
.如果您正在创建用户应该能够修改并视为他/她自己的数据,那么将该数据存储在用户的文档文件夹 (CSIDL_MYDOCUMENTS
) 中是合适的.
These locations are CSIDL_APPDATA
or 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
).
在my在这里回答.
示例代码:
TCHAR szFolderPath[MAX_PATH];
if (!SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_APPDATA, FALSE))
{
// Uh-oh! An error occurred; handle it.
}
或者,使用 MFC 的 CString
类:
Or, using MFC's CString
class:
CString buffer;
BOOL bRet = SHGetSpecialFolderPath(NULL, buffer.GetBuffer(MAX_PATH), CSIDL_APPDATA, FALSE);
buffer.ReleaseBuffer();
if (!bRet)
{
// Uh-oh! An error occurred; handle it.
}
这篇关于如何在代码中正确使用 %USERPROFILE%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!