【问题标题】:UnauthorizedAccessException in UWP Directory.CreateDirectoryUWP Directory.CreateDirectory 中的 UnauthorizedAccessException
【发布时间】:2021-05-07 10:49:24
【问题描述】:

这是一个构建到 Hololens 2 的 Unity 项目。我正在尝试在 Application.PersistentDataPath 中创建一个新文件夹,但由于 UnauthorizedAccessException 而失败。奇怪的是它一直在工作,直到最近才停止了看似无关的更改。

这是失败的函数。

        static DirectoryInfo EnsureDirectory(string subFolder)
        {   
            Debug.Log($"starting EnsureDirectory() for {subFolder}.");
            
            string directoryPath = Path.Combine(Application.persistentDataPath, subFolder.ValidatePath());
            
            Debug.Log($"About to create directory {directoryPath}");
            var dir = Directory.CreateDirectory(directoryPath);
            Debug.Log($"Successfully created directory {dir.FullName}");
            
            return dir;
        }

在 Directory.CreateDirectory 我收到如下错误: Hololens screenshot of debug log

EXCEPTION: UnauthorizedAccessException: Access to the path "C:\" is denied.

此错误仅在部署到设备时发生。在 Unity 编辑器中,它可以完美运行。我也不知道为什么当那不是我想要使用的路径时它会说“C:”。

任何帮助将不胜感激。

【问题讨论】:

  • 这可能是因为它检查每个路径段,从第一个根目录开始,显然是禁止的。试试new DirectoryInfo(Application.persistentDataPath).CreateSubdirectory(...); 看看错误是否消失。
  • subFolder.ValidatePath() 是做什么的?并且directoryPath 已经错了...xxxxxxxxx/LocalState 本身已经持久数据路径...您实际上会在其中创建一个子文件夹...您以@ 的形式传入什么987654328@?好像是空字符串……
  • @derHugo 验证路径只是删除无效字符。有时 subFolder 是空的,在这种情况下,我直接在持久数据路径中创建文件。当该字符串不为空时,它会创建一个新的子文件夹。如果文件夹已经存在,createdirectory 什么也不做。这一切都在编辑器中工作
  • 在编辑器中,您有权在应用程序“沙盒”之外创建文件和文件夹 .. 在 HoloLens 上您没有;)我只能从您的屏幕截图和日志中看到文件名本身似乎是空的,因为它记录了例如ValidatePath() with .. 后面什么都没有
  • DirectoryInfo(Application.persistentDataPath).CreateSubdirectory(...);对我不起作用,同样的错误

标签: c# unity3d uwp hololens


【解决方案1】:

感谢@aybe 的回答。

这可能是因为它检查每个路径段,开始 第一个是根目录,显然是被禁止的。尝试新的 DirectoryInfo(Application.persistentDataPath).CreateSubdirectory(...); 而是查看错误是否消失

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多