1

Hi all: Can anyone tell me how to get local computer name using Adobe AIR.

Please reply me as soon as possible. Thanks in advance.

Mudasir Bhutto
  • 468
  • 1
  • 11
  • 24

2 Answers2

0
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() );
Limitless isa
  • 3,689
  • 36
  • 28
0

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);  
          }  
     }  
}  
aloisdg
  • 22,270
  • 6
  • 85
  • 105
Feltope
  • 1,098
  • 6
  • 9
  • Thanks. But I've already done with that type. Problem is that I want to create application not only for window but also for MAC and linux too. So, that's why I can't follow that method. I don't whether such type of app (hostname) exists in MAC and Linux or not. – Mudasir Bhutto Apr 03 '11 at 15:35
  • 2
    BTW, `file = new File('C:\\Windows\\System32\\hostname.exe');` is very naive way to run something. Windows can be installed in different location. – alxx Apr 03 '11 at 19:17
  • 1
    Exactly. In Windows 2000, there is folder winnt. So, it'll be problem. But we can check whether folder exists or not? It can be useful then but still not perfect solution. – Mudasir Bhutto Apr 06 '11 at 23:53
  • Also won't work on a Mac, Ubuntu or any Windows installation not installed at C:\Windows. – Steve Kuo Jul 08 '11 at 20:52
  • @MudasirBhutto : I have similar requirement. Have you been able to fetch the hostname across different OS ? – Kartik Domadiya Jul 12 '17 at 05:17