首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Unity3D UnauthorizedAccessException:拒绝对路径的访问

Unity3D UnauthorizedAccessException:拒绝对路径的访问
EN

Stack Overflow用户
提问于 2015-07-01 21:41:00
回答 1查看 5.3K关注 0票数 0

我在我的Unity3D游戏中进行了分析,我意识到当我试图在设备上创建一个文件夹时,一些安卓设备会抛出这个Unity3D。

看看抛出异常的代码:

代码语言:javascript
运行
复制
static void SaveToCache(Entry entry, byte[] bytes) {
    if (entry.file != null) {
        string path = Path.GetDirectoryName(entry.file);
        try {
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            File.WriteAllBytes(entry.file, bytes);          
        } catch (Exception ex) {
            Analytics.LogException(ex, "couldn't save to cache: " + path);
        }
    }
}

密码看起来对我很好,但也许我遗漏了什么.它不会扔到我的测试设备-我只有堆栈跟踪从我的分析。

更相关的代码是我如何构建路径:

代码语言:javascript
运行
复制
static readonly char DirectorySeparator = Path.DirectorySeparatorChar;    

static string BundleFile(string id, string languageCode) {
    StringBuilder sb = new StringBuilder();
    sb.Append(BundlePath());
    sb.Append(id);
    sb.Append("_");
    sb.Append(languageCode.ToLower());
    sb.Append(".json");
    return sb.ToString();
}

static string BundlePath() {
    StringBuilder sb = new StringBuilder();
    sb.Append(Application.persistentDataPath);
    sb.Append(DirectorySeparator);
    sb.Append("bundles");
    sb.Append(DirectorySeparator);
    return sb.ToString();
}

如果有人有类似的问题,请告诉我。我不能依赖Application.persistentDataPath吗?是否有更好的方法为缓存文件构建路径?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-17 08:58:00

我通过编写一个用于处理android上持久数据路径的工具包来解决这个问题。这将检查写访问,甚至在磁盘空间耗尽时使用回退。

在这里找到代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31172191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档