尝试从 .NET MAUI 项目访问 iOS 模拟器上的文件时出现 System.IO.DiretoryNotFoundException

问题描述 投票:0回答:1

我正在将文件从照片库复制到本地应用程序位置以进行进一步处理。 不幸的是,复制完成后,我无法访问该文件。

分配 baContent 的行是我收到失败的地方。

    private async Task SaveAttachment()
    {
        if (SelectedChecklistItem?.AttachmentFilePath != null)
        {
            try
            {
                byte[] baContent;
                baContent = await File.ReadAllBytesAsync(SelectedChecklistItem.AttachmentFilePath);

                await _mySvc.SaveAttachment(baContent);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }

完整的错误消息是:

Could not find a part of the path '/Users/{username}/Library/Developer/CoreSimulator/Devices/87FA0141-768B-4C54-94C9-153508726112/data/Containers/Bundle/Application/26E81D7F-0B46-4022-8112-78B42FA9E546/{myappname}/File: /Users/{username}/Library/Developer/CoreSimulator/Devices/87FA0141-768B-4C54-94C9-153508726112/data/Containers/Data/Application/E90F78D7-53F6-4E11-87FC-C9ED1BB329A1/Documents/atp20240708155037.jpeg'.

当我浏览到 Mac 上的位置时,我能够在上述错误中“File:”后面的路径对应的位置看到该文件。

这是创建文件的代码:

 private async Task CopyToLocalAppStorage(string filePrefix, FileResult result)
 {
     if (result != null)
     {
         var attPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
         var fileExt = System.IO.Path.GetExtension(result.FullPath);

         attachmentFilePath = Path.Combine(attPath, $"{filePrefix}{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}{fileExt}");

         using (FileStream SourceStream = File.Open(result.FullPath, FileMode.Open))
         {
             using (FileStream DestinationStream = File.Create(attachmentFilePath))
             {
                 await SourceStream.CopyToAsync(DestinationStream);
             }
         }
     }
 }

我希望此时能够访问该文件 - 因为它是我在这个应用程序中创建的。

maui .net-8.0 system.io.file maui-ios
1个回答
0
投票

与 MAUI 相关的许多事情一样 - 从模拟器中删除应用程序、删除缓存并重新启动解决了问题。我不知道这是否是其中之一,或者是真正有效的某种组合。 抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.