c# - 如何在 xamarin.forms 中获取应用程序本身的目录
问题描述
我在 Xamarin.Forms 项目中为应用程序(不是 Android 或 iOS 版本)创建的 Resources 文件夹中有一个 .txt 文件,我想知道如何在应用程序加载后立即访问该文件。我尝试使用路径“Resources/tips.txt”(“tips”是文件的名称)创建一个 StreamReader,但这不起作用,因为显然,此时的当前目录什么都不是。如何获取应用程序本身的目录以便我可以访问该文件夹?
解决方案
资源不是文件,您不使用文件路径来访问它们
// 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 ();
}
推荐阅读
- python - 来自多个 excels 的子图
- hive - Hive 中 Presto REPLACE 函数的等价物
- ionic-framework - 离子文本到语音中的停止按钮
- reactjs - 如果没有页面nginx + react.js如何给出404错误
- node.js - 我得到 yargs.methodName is not a function with all yargs methods using ES6 imports in NodeJS without Babel
- excel - VLOOKUP,使用未定义的子字符串作为查找值
- computer-science - 一元与二元运算符的简写
- unity3d - 如果我使用 https 与我的网络服务器通信,恶意玩家能否在 POST 中看到游戏发送的内容?
- perl - 如何使用 MOJO::Dom 找到网页的特定部分?
- c# - 贪婪的正则表达式找到最新的大括号