2012-04-05 40 views
0

我想將兩個參數傳遞給nativeProcess。在flex中傳遞參數NativeProcess

雖然我使用帶有參數的windows命令運行exe文件,但它正在工作。 窗口命令是「abc.exe a.txt b.txt」

如何使用flex nativeProcess將兩個參數傳遞給該格式的exe文件?

這是我到目前爲止有:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="windowedapplication1_applicationCompleteHandler(event)"> 
    <fx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 

      private var process:NativeProcess; 
      protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void 
      { 
       if (NativeProcess.isSupported) 
       { 
        callExe(); 
       } 
       else 
       { 
        textReceived.text = "NativeProcess not supported."; 
       } 
      } 

      public function callExe():void 
      { 
       var a_FilePath:String = File.applicationStorageDirectory.resolvePath("dist/a.txt").nativePath; 
       var b_FilePath:File = File.applicationStorageDirectory.resolvePath("dist/b.txt").nativePath; 

       if (Capabilities.os.toLowerCase().indexOf("win") > -1) 
       { 
        var file:File = File.applicationStorageDirectory.resolvePath("dist/abc.exe"); 
       } 

       var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 

//我把線下,它在我的情況下工作

nativeProcessStartupInfo.workingDirectory = File.applicationStorageDirectory.resolvePath();

nativeProcessStartupInfo.executable = file;

   var args:Vector.<String> = new Vector.<String>(); 
       args.push(a_FilePath); 
       args.push(b_FilePath); 

       nativeProcessStartupInfo.arguments = args; 

       process = new NativeProcess(); 
       process.start(nativeProcessStartupInfo); 
       process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData); 
       process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener); 
       process.addEventListener(ProgressEvent.PROGRESS, progressEvent); 
       process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, errorData); 
      } 

      public function inputProgressListener(event:ProgressEvent):void 
      { 
       textReceived.text += "Input Progress Event"; 
      } 
      public function onOutputData(event:ProgressEvent):void 
      { 
       textReceived.text += "Output Event"; 
      } 
      public function progressEvent(event:ProgressEvent):void 
      { 
       textReceived.text += "Progress Event"; 
      } 
      public function errorData(event:ProgressEvent):void 
      { 
       textReceived.text += "Error Event"; 
      } 
     ]]> 
    </fx:Script> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <mx:TextInput id="textReceived" width="352" height="200"/> 

</s:WindowedApplication> 
+1

由於不能保證您將保持外部鏈接活着,因此最好將代碼示例複製到您的問題中。這將有助於其他用戶在未來理解這個問題。 – 2012-04-05 10:44:20

+0

非常感謝您的指導和編輯。我會記得但我找到了我的答案。我將工作目錄分配給nativeProcess,如上所示nativeProcessStartupInfo_Obj.workingDirectory = File.applicationStorageDirectory.resolvePath();並且它正在工作 – Vivek 2012-04-05 11:53:57

+2

然後將您的解決方案發布爲您的問題作爲答案並請接受它。這對未來的參考也是有益的。 – 2012-04-05 12:09:56

回答

2

我解決了我的難題用粗體添加以下行:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="windowedapplication1_applicationCompleteHandler(event)"> 
    <fx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 

      private var process:NativeProcess; 
      protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void 
      { 
       if (NativeProcess.isSupported) 
       { 
        callExe(); 
       } 
       else 
       { 
        textReceived.text = "NativeProcess not supported."; 
       } 
      } 

      public function callExe():void 
      { 
       var a_FilePath:String = File.applicationStorageDirectory.resolvePath("dist/a.txt").nativePath; 
       var b_FilePath:File = File.applicationStorageDirectory.resolvePath("dist/b.txt").nativePath; 

       if (Capabilities.os.toLowerCase().indexOf("win") > -1) 
       { 
        var file:File = File.applicationStorageDirectory.resolvePath("dist/abc.exe"); 
       } 

       var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 

//我把線以下,這在我的情況

nativeProcessStartupInfo.workingDirectory =文件的工作。 applicationStorageDirectory.resolvePath();

nativeProcessStartupInfo.executable = file;

   var args:Vector.<String> = new Vector.<String>(); 
       args.push(a_FilePath); 
       args.push(b_FilePath); 

       nativeProcessStartupInfo.arguments = args; 

       process = new NativeProcess(); 
       process.start(nativeProcessStartupInfo); 
       process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData); 
       process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener); 
       process.addEventListener(ProgressEvent.PROGRESS, progressEvent); 
       process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, errorData); 
      } 

      public function inputProgressListener(event:ProgressEvent):void 
      { 
       textReceived.text += "Input Progress Event"; 
      } 
      public function onOutputData(event:ProgressEvent):void 
      { 
       textReceived.text += "Output Event"; 
      } 
      public function progressEvent(event:ProgressEvent):void 
      { 
       textReceived.text += "Progress Event"; 
      } 
      public function errorData(event:ProgressEvent):void 
      { 
       textReceived.text += "Error Event"; 
      } 
     ]]> 
    </fx:Script> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <mx:TextInput id="textReceived" width="352" height="200"/> 

</s:WindowedApplication>