如何读取Xamarin中资产文件夹中的.lcf文件

时间:2016-02-19 09:25:02

标签: c# xamarin file-read

我已在.lcf文件夹中加载了Assets文件,并将构建操作设置为Content。我得到一个例外,说该文件无法找到。

这是我从assets文件夹中读取.lcf文件并将其存储在byte数组中的方式:

byte[] buf = System.IO.File.ReadAllBytes("custom.lcf");

1 个答案:

答案 0 :(得分:2)

使用AssetManager从资产中读取文件

byte[] buffer = new byte[16*1024];
byte[] data;
AssetManager assets = this.Assets;

using (Stream input = assets.Open ("custom.lcf")) {
  using (MemoryStream ms = new MemoryStream())
  {
      int read;
      while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
      {
          ms.Write(buffer, 0, read);
      }
      data = ms.ToArray();
  }
}