Flex air执行Bat文件,打开关闭文件

来源:互联网 发布:百度人工智能发布会 编辑:程序博客网 时间:2024/06/04 04:57


RunFile.as文件,这个文件负责脚本打开文件,关闭文件操作,里面有调用Bat文件的操作,当然,也是通过NativeProcess类来实现的。
具体代码如下所示:

package com.floor.screen.redevelop.app{import flash.desktop.NativeProcess;import flash.desktop.NativeProcessStartupInfo;import flash.filesystem.File;public class RunFile{//var file:File = new File(); // 文件路径  //C:/Users/LONMID/Desktop/一卡通茶话会_一卡通.docxpublic var urlStr:String = "C:/Users/LONMID/Desktop/结网.pdf"; public  var file:File ;public function RunFile(){file = new File(urlStr); }public function openFile():void{file = new File(urlStr); file.openWithDefaultApplication();}/** * 关闭文件 **/ public function closeFile():void{//file = new File("C:/Users/LONMID/Desktop/ccc.exe"); //file.openWithDefaultApplication();var cmdFile:File=new File();cmdFile = cmdFile.resolvePath("C:/WINDOWS/system32/cmd.exe");var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();nativeProcessStartupInfo.executable = cmdFile;var processArgs:Vector.<String> = new Vector.<String>();processArgs[0] = "/c C:/Users/LONMID/Desktop/ccc.bat "; //上面执行批处理的方式方法// C:/Users/LONMID/Desktop/ccc.batnativeProcessStartupInfo.arguments=processArgs;var process:NativeProcess = new NativeProcess();process.start(nativeProcessStartupInfo); }}}

2.调用这个as的mxml文件,如下所示:
<?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"><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><fx:Script><![CDATA[import  com.floor.screen.redevelop.app.RunFile ;/** * 打开文件测试 **/ protected function button1_clickHandler(event:MouseEvent):void{var f:RunFile = new RunFile();f.openFile();}/** * 打开下一个文件测试 * 要回来半闭生一个文件 **/ protected function button2_clickHandler(event:MouseEvent):void{var f:RunFile = new RunFile();f.closeFile();}]]></fx:Script><s:Button x="112" y="83"  label="txt" click="button1_clickHandler(event)"/><s:Button x="201" y="83" label="paf" click="button2_clickHandler(event)"/></s:WindowedApplication>

注意:也要在配置文件中添加如下代码:
    <supportedProfiles>extendedDesktop</supportedProfiles>