唯一干净的方式来获取用户的配置文件目录是使用SHGetSpecialFolderPath API与适当CSIDL代码(在你的情况CSIDL_PROFILE )。这里是一个简短的(未经测试)例如:
char the_profile_path[MAX_PATH];
if (SHGetSpecialFolderPath(NULL, the_profile_path, CSIDL_PROFILE, FALSE) == FALSE)
{
cerr << "Could not find profile path!" << endl;
return;
}
std::ostringstream the_file;
buffer << the_profile_path << "\\file";
if (DeleteFile(buffer.c_str()) == TRUE)
{
cout << buffer << " deleted" << endl;
}
else
{
cout << buffer << " could not be deleted, LastError=" << GetLastError() << endl;
}
其他各方面为“构建”用户的配置文件路径或Windows的其它任何特殊的文件夹会导致严重的麻烦。例如,如果配置文件的位置在未来版本中发生变化(如在Windows XP和Vista之间发生变化),或者路径的某些部分与语言有关(从Vista以后不再是问题)或用户重新定位配置文件(可能是管理环境中的问题等)
请注意,您应该为应用程序创建文件的位置不是配置文件的根路径,而是AppData或LocalAppData(都可以使用相应的CSIDL文件夹查询。)