来源:http://blog.sina.com.cn/s/blog_a6fb6cc90102uy9k.html
Qt中操作系统环境,官方解释如下:
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:\Users\wangl-d\AppData\Local" "LIB=D:\Program Files\Microsoft Visual Studio 10.0\VC\LIB;D:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib;" "LOGONSERVER=\\GLODON-DC01" "ASL.LOG=Destination=file" "PATH=D:\Qt\Qt5.1.1\5.1.1\msvc2010\lib;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\Glodon\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\RichText\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\quazip\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\Glodon\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\RichText\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\quazip\lib\Debug\X86;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\VLD\lib\Win32;D:\Works\GAEA-PUBLIC\GGM\trunk\lib\VectorDrawEditor\lib\x86;D:\Qt\Qt5.1.1\5.1.1\msvc2010\bin;C:\Program Files (x86)\Microsoft F#\v4.0\;D:\Program Files\Microsoft Visual Studio 10.0\VSTSDB\Deploy;D:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\;D:\Program Files\Microsoft Visual Studio 10.0\VC\BIN;D:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools;C:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\Windows\Microsoft.NET\Framework\v3.5;D:\Program Files\Microsoft Visual Studio 10.0\VC\VCPackages;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\HTML Help Workshop;D:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;D:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\Qtitan\lib;D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\VLD\bin\Win32;D:\Qt\Qt5.1.1\5.1.1\msvc2010\bin;D:\Glodon\GFY2.0_new\Controls\GrandResource\Lib;D:\Glodon\GFY2.0_new\Controls\GAEAResource\GCL\Bpl" "COMMONPROGRAMFILES(X86)=C:\Program Files (x86)\Common Files" "USERDOMAIN=GRANDSOFT" "FRAMEWORKVERSION32=v4.0.30319" "QTDIR=D:\Qt\Qt5.1.1\5.1.1\msvc2010" "WINDOWS_TRACING_LOGFILE=C:\BVTBin\Tests\installpackage\csilogfile.log" " FRAMEWORKDIR=C:\Windows\Microsoft.NET\Framework\" "PROGRAMW6432=C:\Program Files" "TMP=C:\Users\wangl-d\AppData\Local\Temp" "QTITANDIR=D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource\ThirdPart\Qtitan" "FRAMEWORKDIR32=C:\Windows\Microsoft.NET\Framework\" "USERNAME=wangl-d" "FP_NO_HOST_CHECK=NO" "TEMP=C:\Users\wangl-d\AppData\Local\Temp" "FRAMEWORKVERSION=v4.0.30319" "VCINSTALLDIR=D:\Program Files\Microsoft Visual Studio 10.0\VC\" "PROMPT=$P$G" "VSEDEFLOGDIR=C:\ProgramData\McAfee\DesktopProtection" "HOMEPATH=\Users\wangl-d" "WINDOWS_TRACING_FLAGS=3" "GLDRS=D:\Works\GAEA-PUBLIC\GJH\trunk\Imports\GlodonResource" "NUMBER_OF_PROCESSORS=4" "VSINSTALLDIR=D:\Program Files\Microsoft Visual Studio 10.0\" "COMSPEC=C:\Windows\system32\cmd.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 SDKs\Windows\v7.0A\" "HOMEDRIVE=C:" "DEVENVDIR=D:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\" "COMPUTERNAME=WANGL-D-PC" "PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" "APPDATA=C:\Users\wangl-d\AppData\Roaming" "PROGRAMDATA=C:\ProgramData" "PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel" "PUBLIC=C:\Users\Public" "COMMONPROGRAMFILES=C:\Program Files (x86)\Common Files" "OS=Windows_NT" "FRAMEWORK35VERSION=v3.5" "DEFLOGDIR=C:\ProgramData\McAfee\DesktopProtection" "LIBPATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\Windows\Microsoft.NET\Framework\v3.5;D:\Program Files\Microsoft Visual Studio 10.0\VC\LIB;D:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;" "BDSCOMMONDIR=C:\Users\Public\Documents\RAD Studio\5.0" "VS100COMNTOOLS=D:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\" "COMMONPROGRAMW6432=C:\Program Files\Common Files" "USERPROFILE=C:\Users\wangl-d" "WINDIR=C:\Windows" "PROGRAMFILES(X86)=C:\Program Files (x86)" "PSMODULEPATH=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\" "PROCESSOR_ARCHITECTURE=x86" "GRS=D:\Glodon\GFY2.0_new\Controls\GrandResource" "PROCESSOR_ARCHITEW6432=AMD64" "SESSIONNAME=Console" "SYSTEMROOT=C:\Windows" "USERDNSDOMAIN=GRANDSOFT.COM.CN" "ALLUSERSPROFILE=C:\ProgramData" "INCLUDE=D:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE;D:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\INCLUDE;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include;"以上环境变量具体都是什么意思,我就不解释了,见名知义,大家肯定都比我聪明,O(∩_∩)O~。。。