2

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?

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36

1 Answers1

5

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
masoud
  • 55,379
  • 16
  • 141
  • 208
  • Thank you! One question more (I'm curious :) ): What happens if I set the path via `setenv`. Will both paths be changed? – Max Beikirch May 26 '13 at 11:07
  • Nice question, Setting variables by `setenv` will not save them permanently in system and it doesn't matter which of them are changed. – masoud May 26 '13 at 15:55