Xamarin.android 如何将来自资产目录中文本文件的文本存储在字符串变量中?
我正在用xamarin android做一些测试。我在资产文件夹中有一个文本文件,我知道如何访问该文件:Xamarin.android 如何将来自资产目录中文本文件的文本存储在字符串变量中?,xamarin.android,Xamarin.android,我正在用xamarin android做一些测试。我在资产文件夹中有一个文本文件,我知道如何访问该文件: using (Stream myFile= Assets.Open("myFile.txt")) { using (FileStream myDestinationFile= File.Create("myDestinationPath")) { myFile.CopyTo(myDestinationFile); } } 使用此代码,我可以将文件复制
using (Stream myFile= Assets.Open("myFile.txt"))
{
using (FileStream myDestinationFile= File.Create("myDestinationPath"))
{
myFile.CopyTo(myDestinationFile);
}
}
使用此代码,我可以将文件复制到另一个位置,例如“Personal”,因此我可以编辑该文件,因为我知道我无法编辑assets文件夹中的文件
但是我想将文本文件的信息直接传递给字符串变量,因为我不需要编辑文件,只需要访问信息并将该字符串作为方法的参数传递
是否可以使用资产文件夹的文本文件信息设置字符串变量
谢谢
但是我想将文本文件的信息直接传递给字符串变量,因为我不需要编辑文件,只需要访问信息并将该字符串作为方法的参数传递
如果要从Asset文件夹中的文本文件传递字符串,可以使用AssetManager
文本文件:
将生成操作设置为AndroidAssect:
代码:
结果:
但是我想将文本文件的信息直接传递给字符串变量,因为我不需要编辑文件,只需要访问信息并将该字符串作为方法的参数传递
如果要从Asset文件夹中的文本文件传递字符串,可以使用AssetManager
文本文件:
将生成操作设置为AndroidAssect:
代码:
结果:
// Create a new TextView and set it as our view
TextView tv = new TextView(this);
// Read the contents of our asset
string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("TextFile1.txt")))
{
content = sr.ReadToEnd();
}
// Set TextView.Text to our asset content
tv.Text = content;
SetContentView(tv);