QStringList QProcess::systemEnvironment() [static]
Returns the environment of the calling process as a list of key=value pairs. Example:QStringList environment = QProcess::systemEnvironment();// environment = {"PATH=/usr/bin:/usr/local/bin",// "USER=greg", "HOME=/home/gr返回的结果以类似键、值的形式存储。 举例:获取系统盘下面的AppData目录
QString appdataPath(){ QStringList environmentList = QProcess::systemEnvironment(); QString appPath(""); foreach (QString environment, environmentList ) { if (environment.startsWith("APPDATA=", Qt::CaseInsensitive)) { appPath = environment.mid(QString("APPDATA=").length()); break; } } return appPath ;}更简单的可以通过:QString value =QProcessEnvironment::systemEnvironment().value("APPDATA");来获取相应的键对应的值。这样就可以像其它应用程序一样在自己的目录下放置自己所需的资源、配置文件。。。更多操作可参考:QProcess中setEnvironment、setProcessEnvironment等函数!
如果不知道里面都有什么键值,最简单的方法就是全部输出,查看是否存在自己想要获取的内容!
QStringList environmentList = QProcess::systemEnvironment();foreach (QString environment, environmentList ){ qDebug() << environment;}测试结果如下:
"LOCALAPPDATA=C:Userswangl-dAppDataLocal" "LIB=D:Program FilesMicrosoft Visual Studio 10.0VCLIB;D:Program FilesMicrosoft Visual Studio 10.0VCATLMFCLIB;C:Program Files (x86)Microsoft SDKsWindowsv7.0Alib;"以上环境变量具体都是什么意思,我就不解释了,见名知义,大家肯定都比我聪明,O(∩_∩)O~。。。
"LOGONSERVER=\GLODON-DC01" "ASL.LOG=Destination=file" "PATH=D:QtQt5.1.15.1.1msvc2010lib;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceGlodonlibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartRichTextlibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartquaziplibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceGlodonlibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartRichTextlibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartquaziplibDebugX86;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartVLDlibWin32;D:WorksGAEA-PUBLICGGMtrunklibVectorDrawEditorlibx86;D:QtQt5.1.15.1.1msvc2010bin;C:Program Files (x86)Microsoft F#v4.0;D:Program FilesMicrosoft Visual Studio 10.0VSTSDBDeploy;D:Program FilesMicrosoft Visual Studio 10.0Common7IDE;D:Program FilesMicrosoft Visual Studio 10.0VCBIN;D:Program FilesMicrosoft Visual Studio 10.0Common7Tools;C:WindowsMicrosoft.NETFrameworkv4.0.30319;C:WindowsMicrosoft.NETFrameworkv3.5;D:Program FilesMicrosoft Visual Studio 10.0VCVCPackages;C:Program Files (x8 6)HTML Help Workshop;C:Program Files (x86)HTML Help Workshop;D:Program FilesMicrosoft Visual Studio 10.0Team ToolsPerformance Tools;C:Program Files (x86)Microsoft SDKsWindowsv7.0AbinNETFX 4.0 Tools;C:Program Files (x86)Microsoft SDKsWindowsv7.0Abin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)IntelOpenCL SDK3.0binx86;C:Program Files (x86)IntelOpenCL SDK3.0binx64;D:Program FilesTortoiseSVNbin;C:Program Files (x86)Microsoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100DTSBinn;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartQtitanlib;D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartVLDbinWin32;D:QtQt5.1.15.1.1msvc2010bin;D:GlodonGFY2.0_newControlsGrandResourceLib;D:GlodonGFY2.0_newControlsGAEAResourceGCLBpl" "COMMONPROGRAMFILES(X86)=C:Program Files (x86)Common Files" "USERDOMAIN=GRANDSOFT" "FRAMEWORKVERSION32=v4.0.30319" "QTDIR=D:QtQt5.1.15.1.1msvc2010" "WINDOWS_TRACING_LOGFILE=C:BVTBinTestsinstallpackagecsilogfile.log" "FRAMEWORKDIR=C:WindowsMicrosoft.NETFramework" "PROGRAMW6432=C:Program Files" "TMP=C:Userswangl-dAppDataLocalTemp"
"QTITANDIR=D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResourceThirdPartQtitan"
"FRAMEWORKDIR32=C:WindowsMicrosoft.NETFramework" "USERNAME=wangl-d" "FP_NO_HOST_CHECK=NO" "TEMP=C:Userswangl-dAppDataLocalTemp" "FRAMEWORKVERSION=v4.0.30319" "VCINSTALLDIR=D:Program FilesMicrosoft Visual Studio 10.0VC" "PROMPT=$P$G"
"VSEDEFLOGDIR=C:ProgramDataMcAfeeDesktopProtection" "HOMEPATH=Userswangl-d" "WINDOWS_TRACING_FLAGS=3" "GLDRS=D:WorksGAEA-PUBLICGJHtrunkImportsGlodonResource" "NUMBER_OF_PROCESSORS=4"
"VSINSTALLDIR=D:Program FilesMicrosoft Visual Studio 10.0"
"COMSPEC=C:Windowssystem32cmd.exe"
"PROGRAMFILES=C:Program Files (x86)" "SYSTEMDRIVE=C:" "FSHARPINSTALLDIR=C:Program Files (x86)Microsoft F#v4.0"
"PROCESSOR_REVISION=3a09"
"PROCESSOR_LEVEL=6"
"WINDOWSSDKDIR=C:Program Files (x86)Microsoft SDKsWindowsv7.0A" "HOMEDRIVE=C:" "DEVENVDIR=D:Program FilesMicrosoft Visual Studio 10.0Common7IDE"
"COMPUTERNAME=WANGL-D-PC"
"PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" "APPDATA=C:Userswangl-dAppDataRoaming" "PROGRAMDATA=C:ProgramData" "PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel" "PUBLIC=C:UsersPublic" "COMMONPROGRAMFILES=C:Program Files (x86)Common Files" "OS=Windows_NT" "FRAMEWORK35VERSION=v3.5" "DEFLOGDIR=C:ProgramDataMcAfeeDesktopProtection" "LIBPATH=C:WindowsMicrosoft.NETFrameworkv4.0.30319;C:WindowsMicrosoft.NETFrameworkv3.5;D:Program FilesMicrosoft Visual Studio 10.0VCLIB;D:Program FilesMicrosoft Visual Studio 10.0VCATLMFCLIB;"
"BDSCOMMONDIR=C:UsersPublicDocumentsRAD Studio5.0" "VS100COMNTOOLS=D:Program FilesMicrosoft Visual Studio 10.0Common7Tools" "COMMONPROGRAMW6432=C:Program FilesCommon Files" "USERPROFILE=C:Userswangl-d" "WINDIR=C:Windows" "PROGRAMFILES(X86)=C:Program Files (x86)" "PSMODULEPATH=C:Windowssystem32WindowsPowerShellv1.0Modules" "PROCESSOR_ARCHITECTURE=x86" "GRS=D:GlodonGFY2.0_newControlsGrandResource"
"PROCESSOR_ARCHITEW6432=AMD64"
"SESSIONNAME=Console" "SYSTEMROOT=C:Windows" "USERDNSDOMAIN=GRANDSOFT.COM.CN"
"ALLUSERSPROFILE=C:ProgramData"
"INCLUDE=D:Program FilesMicrosoft Visual Studio 10.0VCINCLUDE;D:Program FilesMicrosoft Visual Studio 10.0VCATLMFCINCLUDE;C:Program Files (x86)Microsoft SDKsWindowsv7.0Ainclude;"
更多参考:
注:技术在于交流、沟通,转载请注明出处并保持作品的完整性。作者:╰☆奋斗ing❤孩子`原文:http://blog.sina.com.cn/s/blog_a6fb6cc90102uy9k.html。