Getting computer name in Adobe AIR
Hi all: Can anyone tell me how to get local compu开发者_JAVA技巧ter name using Adobe AIR.
Please reply me as soon as possible. Thanks in advance.
This might do the trick.
Last post on the page.
What I did in Air 2 to accomplish that is the following:
public function getHostName():void {
if(NativeProcess.isSupported) {
var OS:String = Capabilities.os.toLocaleLowerCase();
var file:File;
if (OS.indexOf('win') > -1) {
//Executable in windows
file = new File('C:\\Windows\\System32\\hostname.exe');
} else if (OS.indexOf('mac') > -1 ) {
//Executable in mac
} else if (OS.indexOf('linux')) {
//Executable in linux
}
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var process:NativeProcess = new NativeProcess();
process.addEventListener(NativeProcessExitEvent.EXIT, onExitError);
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutput);
process.start(nativeProcessStartupInfo);
process.closeInput();
}
}
import utls.StringHelper;
public function onOutput(event:ProgressEvent):void {
var strHelper:StringHelper = new StringHelper();
var output:String = event.target.standardOutput.readUTFBytes(event.target.standardOutput.bytesAvailable);
output = strHelper.trimBack(output, "\n");
output = strHelper.trimBack(output, "\r");
trace('"'+output+'"');
}
The package that I used is from the manual:
package utls
{
public class StringHelper
{
public function StringHelper()
{
}
public function replace(str:String, oldSubStr:String, newSubStr:String):String {
return str.split(oldSubStr).join(newSubStr);
}
public function trim(str:String, char:String):String {
return trimBack(trimFront(str, char), char);
}
public function trimFront(str:String, char:String):String {
char = stringToCharacter(char);
if (str.charAt(0) == char) {
str = trimFront(str.substring(1), char);
}
return str;
}
public function trimBack(str:String, char:String):String {
char = stringToCharacter(char);
if (str.charAt(str.length - 1) == char) {
str = trimBack(str.substring(0, str.length - 1), char);
}
return str;
}
public function stringToCharacter(str:String):String {
if (str.length == 1) {
return str;
}
return str.slice(0, 1);
}
}
}
import flash.filesystem.File;
var OS:String = Capabilities.os.toLocaleLowerCase();
function currentOSUser():String {
var userDir:String = File.userDirectory.nativePath;
var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1);
return userName;
}
trace( 'Os : ' + OS );
trace( 'Os Name: ' + currentOSUser() );
精彩评论