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

发布于 2024-09-28 04:52:40 字数 113 浏览 1 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遮了一弯 2024-10-05 04:52:40

SHGetSpecialFolderPathCSIDL 设置为所需的文件夹(可能是 CSIDL_APPDATA 或 CSIDL_LOCAL_APPDATA) 。

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

Use SHGetSpecialFolderPath with a CSIDL set to the desired folder (probably CSIDL_APPDATA or 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().

猫卆 2024-10-05 04:52:40

如果我没记错的话应该是

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

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

If I recall correctly it should just be

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

Edit: Just double-checked, works fine!

水波映月 2024-10-05 04:52:40

使用 %APPDATA% 环境变量可能在大多数情况下都有效。但是,如果您想以官方 Windows 方式执行此操作,则应使用 SHGetFolderPath 函数,根据您的需要传递 CSIDL 值 CSIDL_APPDATACSIDL_LOCAL_APPDATA

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

编辑: Joey 正确地指出它已被 SHGetKnownFolderPath(在 Windows Vista 中)。对我来说是个好消息:-)。

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 SHGetFolderPath function, passing the CSIDL value CSIDL_APPDATA or CSIDL_LOCAL_APPDATA, depending on your needs.

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

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

花心好男孩 2024-10-05 04:52:40

可以使用这些函数

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

您 所以:

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

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);
难如初 2024-10-05 04:52:40

来自 MSDN 的示例代码:

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 = 用户名\应用程序数据。在 Window 10 中为:username\AppData\Roaming

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

您还可以使用:

CSIDL_LOCAL_APPDATA = username\Local Settings\Application Data(非漫游)

Sample code from MSDN:

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_FLAG_CREATE = combine with CSIDL_ value to force folder creation in SHGetFolderPath()

You can also use:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文