如何在 xamarin.forms 中获取应用程序本身的目录

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

我在应用程序的 Xamarin.Forms 项目(不是 Android 或 iOS 版本)中创建的 Resources 文件夹中有一个 .txt 文件,我想知道如何在应用程序加载后立即访问该文件。我试着用路径 "Resources/tips.txt"("tips" 是文件名)创建一个 StreamReader,但这不起作用,因为显然,此时的当前目录什么都没有。如何获取应用程序本身的目录以便我可以访问该文件夹?

Resources 不是文件,您不使用文件路径来访问它们

// 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 ();
}