简体   繁体   中英

How do I get the directory for the app itself in xamarin.forms

I have a.txt file in a Resources folder that I created in the Xamarin.Forms project for the app (not the Android or iOS versions), and I want to know how to access that file as soon as the app loads. I tried just creating a StreamReader with the path "Resources/tips.txt" ("tips" is the name of the file), but that doesn't work because apparently, the current directory at that point is nothing. How do I get the directory of the app itself so I can access that folder?

Resources aren't files, you don't use file paths to access them

// MyClass is a class in the same assembly as your resource
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MyClass)).Assembly;

// see linked docs for notes about resource naming
Stream stream = assembly.GetManifestResourceStream("MyResourceName");

string text = "";

using (var reader = new System.IO.StreamReader (stream))
{  
    text = reader.ReadToEnd ();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM