如何找到“已保存的游戏"文件夹以编程方式在C/C ++中? [英] How to find the "Saved Games" folder programmatically in C/C++?
问题描述
我正在写游戏.我打算将保存的内容保存在保存的游戏"目录中.
I am writing a game. I am planning to store saves in the "saved games" directory.
如何以编程方式找到已保存的游戏"文件夹的位置?
How to find the location of the Saved Games folder programmatically?
它需要在非英语Windows上运行.像%USERPROFILE%\Saved Games
这样的骇客是无法选择的.
It needs to work on non-English Windows. Hacks like %USERPROFILE%\Saved Games
are not an option.
推荐答案
The Saved Games directory can be located with the SHGetKnownFolderPath() function, available since Windows Vista and Windows Server 2008.
请注意,FOLDERID_SavedGames
参数是C ++引用.替换为&FOLDERID_SavedGames
以从C代码调用.
Note that the FOLDERID_SavedGames
argument is a C++ reference. Replace with &FOLDERID_SavedGames
to call from C code.
在我可以找到的第一个在线MSVC编译器上成功测试:
Tested successfully on the first online MSVC compiler I could find:
https://rextester.com/l/cpp_online_compiler_visual
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <stdio.h>
#include <shlobj.h>
#include <objbase.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
int main(void)
{
PWSTR path = NULL;
HRESULT r;
r = SHGetKnownFolderPath(FOLDERID_SavedGames, KF_FLAG_CREATE, NULL, &path);
if (path != NULL)
{
printf("%ls", path);
CoTaskMemFree(path);
}
return 0;
}
这篇关于如何找到“已保存的游戏"文件夹以编程方式在C/C ++中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!