Question

I try to retrieve the path environment variable on Windows. Therefore, I tried

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
  char* path = getenv("Path");
  cout << "current path is:" << path << endl;
  cin.get(); // program shall be closed when it's finished
}

This works fine and gives me a path. I compared it to my actual path and found out that the path I retrieved by this program is the system path. However, I don't want to get the system path but rather the user path. I tried to change the case of "Path" as on my system "path" refers to the user path variable while "Path" refers to the system path variable, but getenv seems to ignore that. How can I get the value of the system path variable?

Was it helpful?

Solution

getenv("PATH"); It will retrieve the system path and user path together.

You need to use Windows registry mechanism to find out user and system path separately. To access them, read the value of PATH from two different positions in the registry:

User variables:

HKEY_CURRENT_USER\Environment

System variables:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top