如何获取当前用户的用户名? [英] How do I get the user name of the current user?

查看:34
本文介绍了如何获取当前用户的用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 C 编程访问 Windows 中的用户名,并使用该名称创建特定文件的路径,如c:\users\john\Roaming.....等等".因此,对于每个系统用户名,例如john"都是不同的.帮我在运行时找到用户名.

I want to access the user name in the Windows using C programming and use that name to create the path to the particular file like "c:\users\john\Roaming.....and so on". So for every system user name e.g "john" is different. Help me to find the user name at run time.

推荐答案

#include  <stdio.h>

int main(void)
{
    printf("%s\n", getenv("USERPROFILE"));  // Print user's home directory.
    return 0;
}

要获取用户名而不是主路径,请将 USERPROFILE 替换为 USERNAME.

To get the user name instead of the home path replace USERPROFILE with USERNAME.

这篇关于如何获取当前用户的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆