How do I get the user name of the current user?

后端 未结 6 1491
醉梦人生
醉梦人生 2021-01-14 20:05

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\"

6条回答
  •  鱼传尺愫
    2021-01-14 20:54

    you could use the following code to get the Username.

        #include 
    
        void main(void)
        {
            //following gets the appdata folder
            char szAppData[1024];
            char * szBufer      = 0;
            szBufer = getenv ("APPDATA");
            if (szBufer != NULL)
            {
               strcpy(szBufer , szAppData);
            }
    
            //following code gets the user name
            char szOSUserName[1024];
            szBufer = getenv ("USERNAME");
            if (szBufer != NULL)
            {
                strcpy(szBufer , szOSUserName);
            }
        }
    

提交回复
热议问题