windows 如何在 C 中获取 %AppData% 文件夹?

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

How to get the %AppData% folder in C?

cwindowsappdata

提问by Romulus

As above, how do I get the AppData folder in Windows using C?

如上所述,如何使用 C 获取 Windows 中的 AppData 文件夹?

I know that for C# you use Environment.SpecialFolder.ApplicationData

我知道对于 C# 你使用 Environment.SpecialFolder.ApplicationData

回答by Ferruccio

Use SHGetSpecialFolderPathwith a CSIDLset to the desired folder (probably CSIDL_APPDATA or CSIDL_LOCAL_APPDATA).

使用SHGetSpecialFolderPath并将CSIDL设置为所需的文件夹(可能是 CSIDL_APPDATA 或 CSIDL_LOCAL_APPDATA)。

You can also use the newer SHGetFolderPath()and SHGetKnownFolderPath()functions. There's also SHGetKnownFolderIDList()and if you like COM there's IKnownFolder::GetPath().

您还可以使用较新的SHGetFolderPath()SHGetKnownFolderPath()函数。还有SHGetKnownFolderIDList(),如果你喜欢 COM,还有IKnownFolder::GetPath()

回答by Mr. Llama

If I recall correctly it should just be

如果我没记错的话应该是

#include <stdlib.h>
getenv("APPDATA");

Edit: Just double-checked, works fine!

编辑:只是仔细检查,工作正常!

回答by Matt Solnit

Using the %APPDATA%environment variable will probably work most of the time. However, if you want to do this the official Windows way, you should use use the SHGetFolderPathfunction, passing the CSIDL value CSIDL_APPDATAor CSIDL_LOCAL_APPDATA, depending on your needs.

%APPDATA%大多数情况下,使用环境变量可能会起作用。但是,如果您想以官方 Windows 方式执行此操作,则应使用SHGetFolderPath函数,传递 CSIDL 值CSIDL_APPDATACSIDL_LOCAL_APPDATA,具体取决于您的需要。

This is what the Environment.GetFolderPath()method is using in .NET.

这就是该Environment.GetFolderPath()方法在 .NET 中使用的方法。

EDIT:Joey correctly points out that this has been replaced by SHGetKnownFolderPathin Windows Vista. News to me :-).

编辑:Joey 正确地指出这已被Windows Vista 中的SHGetKnownFolderPath取代。给我的消息:-)。

回答by Matt Joiner

You might use these functions:

您可能会使用这些功能

#include <stdlib.h>
char *getenv( 
   const char *varname 
);
wchar_t *_wgetenv( 
   const wchar_t *varname 
);

Like so:

像这样:

#include <stdio.h>
char *appData = getenv("AppData");
printf("%s\n", appData);

回答by Thiago Falcao

Sample code:

示例代码:

TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL,
   CSIDL_APPDATA | CSIDL_FLAG_CREATE,
   NULL,
   0,
   szPath)))
{
   PathAppend(szPath, TEXT("MySettings.xml"));
   HANDLE hFile = CreateFile(szPath, ...);
}

CSIDL_APPDATA = username\Application Data. In Window 10 is: username\AppData\Roaming

CSIDL_APPDATA = 用户名\应用程序数据。在 Window 10 中是:username\AppData\Roaming

CSIDL_FLAG_CREATE = combine with CSIDL_ value to force folder creation in SHGetFolderPath()

CSIDL_FLAG_CREATE = 与 CSIDL_ 值结合以强制在 SHGetFolderPath() 中创建文件夹

You can also use:

您还可以使用:

CSIDL_LOCAL_APPDATA = username\Local Settings\Application Data (non roaming)

CSIDL_LOCAL_APPDATA = 用户名\本地设置\应用程序数据(非漫游)