Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 未找到Android文本文件_C#_Android_Xamarin_Xamarin.forms - Fatal编程技术网

C# 未找到Android文本文件

C# 未找到Android文本文件,c#,android,xamarin,xamarin.forms,C#,Android,Xamarin,Xamarin.forms,我的解决方案中有一个名为“txtWords.txt”的文本文件,我试图用它读取: string path=Environment.GetFolderPath(Environment.SpecialFolder.Personal) stringfilepath=Path.Combine(路径,“txtWords.txt”) 在我的实验中,我在我的C#应用程序和App.Android>Assets文件夹(设置为AndroidAsset)下都有相同的文件。为了安全起见,我想把它放在两个地方 Syste

我的解决方案中有一个名为“txtWords.txt”的文本文件,我试图用它读取:

string path=Environment.GetFolderPath(Environment.SpecialFolder.Personal)

stringfilepath=Path.Combine(路径,“txtWords.txt”)

在我的实验中,我在我的C#应用程序和App.Android>Assets文件夹(设置为AndroidAsset)下都有相同的文件。为了安全起见,我想把它放在两个地方

System.IO.FileNotFoundException:找不到文件“/data/user/0/com.WSC/files/txtWords.txt”

我读过有关使用AssetManager的内容,但这给了我一个“找不到命名空间错误”


我做错了什么?读取应用程序文本文件的最佳方式是什么?这应该很容易,所以我想我犯了一些基本错误。

使用Xamarin Essentials,您可以通过NetStd(Xamarin.Forms)库读取捆绑/资产只读文件:

关于:


Nuget:

如果您试图从PCL/NetStd(Xamarin.Forms)库或Xamarin.Android库执行此操作,则解决方案中的文件不包含在个人文件夹中,它将嵌入应用程序包中。对于Android来说,实现你想要的最好方法就是使用资产。有没有关于如何使用资产的好例子?我已经试过了,但不明白如何正确使用资产名称空间。谢谢。这似乎是答案。尽管奇怪,我现在得到了“wait”操作符只能在异步方法中使用”错误,即使使用了OpenAppPackFileAsync。这是另一个需要调查的问题!
string wordsText;
using (var stream = await FileSystem.OpenAppPackageFileAsync("txtWords.txt"))
using (var reader = new StreamReader(stream))
{
    var wordsText = await reader.ReadToEndAsync();
}