Get windows Environment Variables in AIR application

Writing Desktop application in AIR has certain security limitations, especially when your application will run under Vista or Windows 7 OS. For example, if there is an external process you want to invoke from Flex, sometimes it requires administrator access. In that case you should run the AIR application in administrator mode and this will solve the problem.

But let's take more complicated case - you have no idea of exact path to external application. Meaning, no absolute path can be used - the external tool can be installed in any folder. There is a standard solution for this kind of cases, when the path to the application appears in windows Environment Variables. So you can just get the path by request of specific environment variables from OS.... Unfortunately, you can't. There is no such object in Flex that could make our life easier.


I’m not sure the way I found is a best one, but it works. We can use external batch file (pathfinder.bat) which will request and print to STDOUT required variable:



To run the .bat file we’ll have to use cmd.exe as NativeProcess (we cannot run .bat file directly from AS). Luckily, it’s always there, in "C:\\Windows\\System32\\cmd.exe", no matter what kind of windows, x32 or x64.

We just run the cmd.exe, catch STDOUT with all outputs and parsing to get required environment variables:

private function getPath():void
	var pathFinder:NativeProcess = new NativeProcess;
			pathFinder.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, handlePathError);
			pathFinder.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, handlePathOut);
	var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
	var file:File =  new File("C:\\Windows\\System32\\cmd.exe"); 
	var args:Vector.<String> = new Vector.<String>();
	args.push(File.applicationDirectory.nativePath +  \\bat\\pathfinder.bat");
	startupInfo.arguments = args;
	startupInfo.executable = file;
		if (NativeProcess.isSupported)
	catch (error:Error)
	{"Failed to run pathfinder",	'ERROR');
private function handlePathError(pe:ProgressEvent):void
	var stdio:IDataInput = (pe.currentTarget as NativeProcess).standardError;
	var error:String = stdio.readUTFBytes(stdio.bytesAvailable);,	'ERROR');
private function handleDbengPathOut(pe:ProgressEvent):void
	var stdio:IDataInput = (pe.currentTarget as NativeProcess).standardOutput;
	var output:String = stdio.readUTFBytes(stdio.bytesAvailable);
	var outArr:Array = output.split("\n");
	for (var i:int=0; i < outArr.length; i++)
		if ((outArr[i] as String).indexOf("TOOL1_PATH=") > -1)
			toolOnePath = StringUtil.trim((outArr[i] as String).split("=")[1]);
		if ((outArr[i] as String).indexOf("TOOL2_PATH=") > -1)
			toolTwoPath = StringUtil.trim((outArr[i] as String).split("=")[1]);	


It's ugly. But it works