获取环境变量



#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;


//获取环境变量
string getEnv(string strEnvName)
{
    char cEnvValue[256];
    size_t requiredSize = 500;
    getenv_s(&requiredSize, cEnvValue, requiredSize, strEnvName.c_str());
    string strEnvValue(cEnvValue);
    return strEnvValue;
}

int main(int argc, char *argv[])
{
    //获取环境变量temp的值 C:\Users\userneme\AppData\Local\Temp
    string strTemp = getEnv("temp");
    cout << strTemp << endl;

    cin.get();
    return 0;
}