[英]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.