iOS .NET MAUI 中的资源文件夹路径位置

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

我正在构建一个射箭应用程序来记录分数。我有一个名为 ScoreData 的文件夹 reData,我正在其中写入文件并存储。评分完成后,我想将数据写入所述文件夹中的一个简单的 .txt 文件。对于我的 Windows 调试,它使用 GetEntryAssembly 搜索解决方案,并每次向上查找一个文件夹,直到找到 ScoreData 文件夹。当使用“Windows Machine”进行调试时,此解决方案不会给我带来任何错误,但是当我连接 iPhone 进行调试时,它会在 GetDirectories() 方法上引发错误。

这是我当前的代码,它可以在 Windows 上运行,但在 iOS 设备上调试时会抛出错误。

`var anotherDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assembly = typeof(App).GetTypeInfo().Assembly;
//var location = assembly.Location;  
//var dirst = Assembly.GetEntryAssembly();  
//var dires = Directory.GetDirectories(dirst.ToString());  
var directory = new DirectoryInfo(anotherDir ?? Directory.GetCurrentDirectory());
var path = string.Empty;
while (directory != null && !directory.GetFiles("\*.sln").Any())
{
     directory = directory.Parent;
     var folders = directory.GetDirectories();
     if (folders.Length == 9)
     {
          var folder = folders\[6\];
          path = folder.FullName;
     }
}
return path;
`

这是我在 iOS 中调试时想要获取文件路径的屏幕截图。 在此输入图片描述

c# ios .net maui
1个回答
0
投票

看起来您的文件在解决方案中相对存在。将 .txt 添加为 MauiAsset 并在您的 csproj 中设置以下内容


<MauiAsset Include="MyFolder\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />

然后您可以使用

读取文件
   public async Task<string> ReadTextFile(string filePath)
   {
      using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath);
      using StreamReader reader = new StreamReader(fileStream);
      return await reader.ReadToEndAsync();
   }
© www.soinside.com 2019 - 2024. All rights reserved.