代码之家  ›  专栏  ›  技术社区  ›  Jordan.J.D

使用as3打开文件位置

  •  1
  • Jordan.J.D  · 技术社区  · 9 年前

    我有一个 <s:List /> 包含一堆 files 。右键单击时,我在鼠标的(x,y)位置打开一个菜单,允许用户“打开文件位置”。我的挣扎是打开文件位置并选择(不打开)文件,就像Window的资源管理器一样。我最近打开父文件夹并使用 file.openWithDefaultApplication(); ,打开文件所在的文件夹,而不向用户显示实际文件。

    mxml语言

            <s:List
                id="fileDownList"
                height="100%"
                width="100%"
                dataProvider="{files}"
                labelField="name"
                allowMultipleSelection="false"
                rightClick="rightMouseDown(event)"
                itemRollOver="currentItem = event.itemRenderer.data"
                />
    

    作为3

        private function rightMouseDown(event:MouseEvent):void {
            createMenu(currentItem, event.stageX, event.stageY);
        }
    
            private function createMenu(item:Object, xPos:int, yPos:int):void {
            if (menu != null) {
                menu.hide();
            }
            var menuItems:Array = [];
    
            menuItems.push({label:"Open File Location"),
                func: function run():void{
                    //runs on doMenuAction listener, need to open location here
    
                }
    
            });
    
            if (menuItems.length > 0) {
                menu = Menu.createMenu(tree, menuItems);
                //noinspection JSValidateTypes
                menu.addEventListener(MenuEvent.ITEM_CLICK, doMenuAction);
            }
    
            if (menu != null) {
                menu.show(xPos, yPos);
            }
    
        }
    

    实例

    enter image description here enter image description here

    2 回复  |  直到 9 年前
        1
  •  2
  •   VC.One    9 年前

    试一试…结果 这是可能的 使用NativeProcess和一些 Explorer.exe 参数。

    这是一个仅限AS3的基本示例。请测试并应用代码中的逻辑:

    //# String holds required file path
    //# example ::: myfileURL = "C:\\myFolder\\mySubFolder\\myImage.jpg";
    public var myfileURL : String = "";
    
    myfileURL = "C:\\VC1\\Tests\\CoolSong.mp3"; //update this before running function
    openWindows_FileSelected(); //run the function
    
    private function openWindows_FileSelected ():void  
    {  
        var explorer:File = new File("C:\\Windows\\explorer.exe");
    
        if (explorer.exists)  
        {  
            var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();  
            nativeProcessStartupInfo.executable = explorer;  
    
            var args:Vector.<String> = new Vector.<String>();  
    
            args.push("/select,"); 
            args.push( myfileURL ); //file to auto-highlight
    
            nativeProcessStartupInfo.arguments = args;  
            process = new NativeProcess();  
            process.start(nativeProcessStartupInfo);  
        }  
    
    } 
    


    PS:

    我能想到的唯一难题…因为你在使用 File 您必须通过文件获取其路径的字符串 .nativePath 命令,该命令给出如下字符串:
    "C:/myFolder/mySubFolder/myImage.jpg"

    但是要使上面的代码工作,您必须执行替换(请尝试String方法 Split/Join )看起来像:
    "C:\\myFolder\\mySubFolder\\myImage.jpg" .
    如果你不用双反斜杠替换所有的单正斜杠,那么 资源管理器.exe 如果你不喜欢它,你总是会出错。。。

        2
  •  1
  •   Jordan.J.D    9 年前

    我最后做的是创建一个.cmd文件(只是一个重命名的.bat文件),它打开一个目录 /select 参数。

    作为3

    private function run():void{
                            var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                            var file:File = File.applicationDirectory.resolvePath("C:\\Users\\Me\\Desktop\\launcher.cmd");
                            nativeProcessStartupInfo.executable = file;
    
                            var processArgs:Vector.<String> = new Vector.<String>();
                            processArgs[0] = item.url;
                            nativeProcessStartupInfo.arguments = processArgs;
    
                            process = new NativeProcess();
                            process.start(nativeProcessStartupInfo);
    
                        }
    

    发射器.cmd

    @ECHO OFF
    SET /a LOCATION=%1
    explorer /select, %1