简体   繁体   中英

How to find get the Desktop address for a user on Windows?

I was trying to write a C code to create a file on the Desktop. I used getenv() to get the username for the address, but it is printing some weird symbols.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
    char raw[] = "C://Users/%/Desktop/xyz.txt";
    char loc[strlen(raw)+strlen(getenv("USERNAME"))-1] = "";
    char *env = ""; 
    strcpy(env ,getenv("USERNAME"));

    for (int i = -1; i < sizeof(raw); ++i){
        if (raw[i]!='%'){
            strcpy(loc, raw[i]);
        }
        else if (raw[i]=='%'){
            strcpy(loc, env);
        }
    }

    printf(loc);
    return 0;
}

This is the code I wrote(please forgive the variable names). And This is what I got: Évs♦↨║╨↕@

I am using GCC.

PS I am new to the language, so sorry if it's a silly question.

Edit - I forgot to mention that I wanted to get a variable with the address, so I could use it for other things.

When in doubt, check the Qt sources for these kinds of things. At least that's what I do when looking for this type of stuff.

You can find the way they do it here . Mind you, it's written in C++, but the OS details usually boil down to plain C at some point, because that's the low-level API provided by the operating system (yes, even on Mac, even though that takes some figuring out to get everything right).

Basically, they call shGetKnownFolderPath from Shell32.dll with the GUID FOLDERID_Desktop .

Note that what you're doing is fine in a sense, but relying on environment variables is somewhat dangerous, and they don't cater for a lot of things you might want to get.

At:

    strcpy(env ,getenv("USERNAME"));

the pointer env is unitialised. It needs to reference specifically allocated memory to which strcpy() can copy the string.

Other then that you have in any case somewhat over-complicated the solution. Consider:

#include <stdio.h>
#include <windows.h>

int main()
{
    char loc[MAX_PATH + 1] ;
    sprintf( loc, "C:\\Users\\%s\\Desktop\\xyz.txt",  getenv("USERNAME") ) ;
    printf( "%s\n", loc);

    return 0;
}

However it is a flawed assumption. My desktop folder for example is on D:\Users\%USERNAME%\Desktop - a user can move their special folders to any location they wish (as I have). Moreover different versions of Windows have in any case placed it various other default locations. For example on XP C:\Documents and Settings\%USERNAME% .

You should ask the OS where its OS special folders are. For example using SHGetFolderPathA() :

#include <stdio.h>
#include <windows.h>
#include <Shlobj.h>

int main()
{
    char loc[MAX_PATH + 1] ;
    SHGetFolderPathA( NULL, CSIDL_DESKTOP, NULL, 0, loc) ; 
    printf( "%s\n", loc);

    return 0;
}

This one is specific to windows. USERPROFILE environment variable should get the path for user home in which case desktop path can be calculated as below.

char desktop_path[MAX_PATH + 1] ;
sprintf( desktop_path, "%s\\Desktop", getenv("USERPROFILE") ) ;

char filepath[MAX_PATH + 1] ;
sprintf( filepath, "%s\\%s", desktop_path, "whateverfile.txt" ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM