Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c# - 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

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

The image below is showing the code ive used to try and access file and the folder where the file is placed

How should I 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?

question from:https://stackoverflow.com/questions/66066544/how-to-access-files-in-xamarin-android-and-also-in-visual-studio-solution-explo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Xamarin.Android project, Assets are read using an AssetManager. An instance of the AssetManager is available by accessing the Assets property on an Android.Content.Context, such as an 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...