简体   繁体   中英

How to find desktop path using C language?

Can anybody please tell me how I can get the desktop path(environment variable) using C language commands.

Scenario is that I want to save a file on desktop.What I can do is just to give a fixed desktop path and a filename to save the file. But after giving this fixed path ,my code will become rigid and will not work on anyother computer having different desktop path (environment variable). My question is can I make my code generalize which can work on any windows based machine by capturing the environment variable of desktop , using C language?

Regards

You can use the function getenv() to access environment variables:

The value of an environment variable can be accessed with the getenv function. This is declared in the header file stdlib.h.

For Windows systems, USERPROFILE should be good if you append \\Desktop to it (see the link provided by mgiuca, for example). Note that the path will contain spaces in most cases, so handle it accordingly.

Also, using environment variables to get the path of the desktop folder isn't the best way and might not work in some cases (also see other SO answers to this topic, like this one ), so see this answer as an answer to your specific question, especially for the "environment variables in C++" part.

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