qt 快速获取环境变量中所有的键对值 以及所对应的value 如:键值:path 对应 value:C:\Windows\system32 方便安装外部设备时判断设备依赖是否安装
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QLibrary>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcessEnvironment systemEnv = QProcessEnvironment::systemEnvironment();
if(!systemEnv.isEmpty())
{
qDebug() << "systemEnv" << systemEnv.keys();
}
// 检查特定依赖项是否存在 自己设置自己的键对值
QString dependency = "PATH";
if (systemEnv.contains(dependency))
{
QString value = systemEnv.value(dependency);
QStringList value_list = value.split(";");
qDebug() << "value_list" << value_list ;
QString dll_path = "C:\\Windows\\system32";
if(value_list.contains(dll_path))
{
qDebug() << "dll_path" << dll_path <<"is exit" ;
QString dll_load_path = dll_path + "某个依赖的.dll";
QLibrary test_dll(dll_load_path);
qDebug() << "dll_load_path" << dll_load_path <<"is exit" ;
if(test_dll.load())
{
qDebug() << "is exit" ;
}
else
{
qDebug() << "is fail" ;
}
}
else
{
qDebug() << "dll_path" << dll_path <<"is not exit" ;
}
} else
{
qDebug() << "Dependency" << dependency << "is not set";
}
return a.exec();
}