VS2019 Xamarin Android读写TXT文件
作者: 葛屹肃 | 日期: 2023-10-27 | 分类: 知识经验
项目需要,开发安卓APP要进行TXT文件读写,在官网(点击查看)上找到方法,经测试可行,记录一下方法,以免以后找不到了。
读文件:
public async Task SaveCountAsync(int count)
{
var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), “count.txt”);
using (var writer = File.CreateText(backingFile))
{
await writer.WriteLineAsync(count.ToString());
}
}
写文件:
public async Task<int> ReadCountAsync()
{
var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), “count.txt”);if (backingFile == null || !File.Exists(backingFile))
{
return 0;
}var count = 0;
using (var reader = new StreamReader(backingFile, true))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (int.TryParse(line, out var newcount))
{
count = newcount;
}
}
}
return count;
}
以上代码在VS2019编译通过。
文章链接:https://www.geyisu.com/3279.html