Reputation: 541
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
Upvotes: 4
Views: 5669
Reputation: 49719
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.
Upvotes: 4