Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在xamarin.forms中获取应用程序本身的目录_C#_Xamarin.forms_File Io - Fatal编程技术网

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

C# 如何在xamarin.forms中获取应用程序本身的目录,c#,xamarin.forms,file-io,C#,Xamarin.forms,File Io,我在资源文件夹中有一个.txt文件,是我在Xamarin.Forms项目中为应用程序而不是Android或iOS版本创建的,我想知道在应用程序加载后如何访问该文件。我尝试创建一个StreamReader,路径Resources/tips.txt tips是文件名,但这不起作用,因为显然,此时的当前目录是空的。如何获取应用程序本身的目录以便访问该文件夹?不是文件,您不使用文件路径访问它们 // MyClass is a class in the same assembly as your reso

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

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

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

非常感谢你。这解决了我的问题,同时查找了其中的一些部分,因为我对嵌入式资源还不熟悉。