我想使用C编程访问Windows中的用户名,并使用该名称创建特定文件的路径,如"c:\ users\john\Roaming .....等".因此对于每个系统用户名,例如"john"是不同的.帮我在运行时找到用户名.
#include <stdio.h>
int main(void)
{
printf("%s\n", getenv("USERPROFILE")); // Print user's home directory.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
获取用户名而不是主路径替换USERPROFILE
为USERNAME
.
你在寻找什么,这里可能更多SHGetKnownFolderPath
.该功能可让您查找每个用户的特殊文件夹.这是查询用户名的首选,因为主文件夹可能与用户名称不同.
WSTR* location;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &location);
if (SUCCEEDED(hr))
{
// location contains the folder path
// call CoTaskMemFree to free up the memory once you're done with it
CoTaskMemFree(location);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8757 次 |
最近记录: |