简体   繁体   English

如何访问 xamarin.android 中的文件。 还有在 Visual Studio 解决方案资源管理器中,我应该将文件放在哪里以便在我的代码中找到它

[英]how to access files in xamarin.android. And also in Visual Studio Solution explorer where should i place the file in order to locate it in my code

The code below tries to access file named zimbabwe in Asset folder下面的代码尝试访问 Asset 文件夹中名为 zimbabwe 的文件

var fileContent = File.ReadAllText("Asset/zimbabwe.shp")

下图显示了用于尝试访问文件的代码以及放置文件的文件夹

How should I access files in xamarin.android?我应该如何访问 xamarin.android 中的文件? And also in Visual Studio Solution explorer where should I place the file in order to locate it in my code?还有在 Visual Studio 解决方案资源管理器中,我应该将文件放在哪里以便在我的代码中找到它?

In Xamarin.Android project, Assets are read using an AssetManager .在 Xamarin.Android 项目中,使用AssetManager读取Assets An instance of the AssetManager is available by accessing the Assets property on an Android.Content.Context , such as an Activity. AssetManager的实例可通过访问Android.Content.Context上的 Assets 属性获得,例如 Activity。

For example:例如:

string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("zimbabwe.shp")))
{
    content = sr.ReadToEnd ();
}

And in Xamarin.Forms project,you could also add any file into the Assets folder in the Android project and mark the Build Action as AndroidAsset to use it with OpenAppPackageFileAsync .在 Xamarin.Forms 项目中,您还可以将任何文件添加到AndroidAsset项目的Assets文件夹中,并将Build Action标记为OpenAppPackageFileAsync

using (var stream = await FileSystem.OpenAppPackageFileAsync(templateFileName))
{
  using (var reader = new StreamReader(stream))
  {
    var fileContents = await reader.ReadToEndAsync();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在解决方案资源管理器中合并我的代码隐藏文件? - How do I merge my code behind file in the solution explorer? 我在 Visual Studio 中找不到我的解决方案资源管理器 - I cannot find my solution explorer in visual studio 如何在visual studio 2013解决方案资源管理器中获取文件路径? - How to get a file path in visual studio 2013 solution explorer? 如何在运行时将 c# 文件添加到 Visual Studio 解决方案资源管理器? - How to add a c# file to Visual Studio Solution Explorer at runtime? 如何在Xamarin.Android中的ListActivity上的项目上添加图标或图片。 - How can I add Icons or pictures to items on ListActivity in Xamarin.Android. 解决方案资源管理器中的 Visual Studio 2017 锁定文件 - Visual Studio 2017 lock file in solution explorer 配置文件未出现在 Visual Studio 的解决方案资源管理器中 - Config file not appearing in solution explorer in visual studio Xamarin.Android 中的壁纸功能。 它是如何工作的以及如何使用它? - wallpaper function in Xamarin.Android. How does is work and how to use it? 为什么我的Visual Studio在解决方案资源管理器中不显示解决方案? - Why does my Visual Studio not show a solution in the Solution Explorer? 无法访问Visual Studio 2010解决方案资源管理器中的文件夹 - Cannot access folders in Visual Studio 2010 Solution Explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM