How to read asset files using VS & Monodroid

I am trying to implement this example using C # and Monodroid, but I am having difficulty reading and writing the Asset file:

http://docs.xamarin.com/android/advanced_topics/using_android_assets

I use an emulator, not a device.

First of all, it's hard for me to find the namespace for Assets.Open. I ended up finding

const string lfn  = MyAssetFile.txt;
System.IO.StreamReader(Android.Content.Res.Resources.System.Assets.Open(lfn);

Is this the correct namespace?

Secondly, my Asset file is marked as AndroidAsset and "Copy Always" in the VS Properties panel, but my attempts to read the file always fail (File Not Found) with this statement:

string  settings = "";
using (StreamReader sr = new System.IO.StreamReader (Android.Content.Res.Resources.System.Assets.Open(lfn))) settings   =   sr.ReadToEnd();

I have the wrong VS settings, so the asset file is not copied to the emulator or it is copied OK, but my code to open / read is wrong?

+5
2

:

const string lfn = "MyAssetFile.txt";
string settings = string.Empty;

// context could be ApplicationContext, Activity or
// any other object of type Context
using (var input = context.Assets.Open(lfn))
using (StreamReader sr = new System.IO.StreamReader(input))
{
    settings = sr.ReadToEnd();
}

, IDE, Android.Content.Res.Resources.System.Assets Android, . , AssetManager Activities Contexts.

: Activity Assets. , . View Context.Assets.

+9

:

System.IO;

.

Xamarin using .

-4

All Articles