【问题标题】:Renaming a file in as3 AIR - how to do it if you have file's native path in a var?重命名 as3 AIR 中的文件 - 如果 var 中有文件的本机路径,该怎么办?
【发布时间】:2013-11-17 07:25:13
【问题描述】:

如果您知道文件位于 applicationStorageDirectory 中,则以下代码非常适合重命名文件

var sourceFile:File = File.applicationStorageDirectory;
sourceFile = sourceFile.resolvePath("Kalimba.snd");
var destination:File = File.applicationStorageDirectory;
destination = destination.resolvePath("test.snd");

try  
{
    sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
    trace("Error:" + error.message);
}

如果你只有文件的本地路径,你如何设置 sourceFile?像这样:

D:\Software\files\testList.db

这会引发错误:

sourceFile = sourceFile.resolvePath("D:\Software\files\testList.db");

我的想法是我想重命名我之前加载到 var 中的文件。我想我会提取到 String var 的本机路径,将 File var 设为空(因此操作系统不会告诉我在 Flash 中打开文件时它不能重命名),将该 nativePath 解析为 sourceFile,然后使用 moveTo 重命名硬盘上的文件。

为看一看干杯。

编辑:

我已经设置了一个测试 AIR 应用程序,其中只有以下内容:

import flash.events.*;
import flash.filesystem.*;

var original = File.documentsDirectory; 
original = original.resolvePath("D:\\Software\\test\\October.db"); 

var destination:File = File.documentsDirectory; 
destination =  destination.resolvePath("copy.db"); 

original.addEventListener(Event.COMPLETE, fileMoveCompleteHandler); 
original.addEventListener(IOErrorEvent.IO_ERROR, fileMoveIOErrorEventHandler); 
original.moveToAsync(destination); 

function fileMoveCompleteHandler(event:Event):void { 
    trace(event.target); // [object File] 
} 
function fileMoveIOErrorEventHandler(event:IOErrorEvent):void { 
    trace("I/O Error.");  
} 

这会失败,使用 D:\Software\test\October.db 也会失败

我想我想知道什么 - 如果您已经知道完整路径,您将如何做 resolvePath 事情?

【问题讨论】:

  • 所以你已经有一个File 类实例链接到你要重命名的文件?使用它来重命名应该没有问题,一个文件在你真正打开它之前不是“正在使用”的(例如使用流)。
  • 我就是这么想的。然而,moveTo 失败了,尽管 copyTo 成功了。
  • 您确定文件确实被锁定了吗?也许您只是没有对该特定文件/文件夹的写权限?还有问题发生的确切代码是什么样的?你到底收到了什么错误?为了完整起见(不确定是否只是复制和粘贴错误),在字符串文字中,反斜杠必须转义才能充当反斜杠,即您的路径应类似于 D:\\Software\\files\\testList.db
  • 我已经设置了一个测试 AIR 应用程序,它具有以下特性:
  • 这不应该有任何区别,您应该能够从任何文件对象解析绝对路径,原始路径仅在解析相对路径时才重要。虽然查看您的代码没有必要使用resolvePath(),但您可以将绝对路径传递给File constructor,除了您的代码sn-p 对我来说很好。顺便说一句,如果您遇到 I/O 错误,那么您应该跟踪实际错误,即trace(event)

标签: air file-rename


【解决方案1】:

我猜我想知道什么 - 如果您已经知道完整路径,您将如何处理 resolvePath?

你不相信。如果你的路径实际上是 d:\software\test\october.db 你可以设置一个 File ref 像:

var original:File = new File();
original.nativePath = "d:\software\test\october.db";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2014-05-19
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多