我无法使文件处理工作并已关注所有帖子

I cannot get file handling to work and have followed all the posts

我通过添加了一个文本文件正如你从我的代码中看到的那样,我实现了他们的代码。在 运行 上,它收到 System.AggregateException 消息 "access denied to path"

我阅读了帖子:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows https://forums.xamarin.com/discussion/64887/how-to-read-write-files

这是我的代码:

using System.IO;
using System.Reflection;
using System.ComponentModel;

using Xamarin.Forms;
using System;
using System.Threading.Tasks;

namespace ReadTxt
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {


        public MainPage()
        {
            InitializeComponent();



                Label l = new Label()
                {
                    Text = ReadTextLine().Result
                };
                StackLayout s = new StackLayout();
                s.Children.Add(l);
            Content = new ScrollView { Content = s };

            async Task<string> ReadTextLine()
            {
                var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");
                using (var writer = File.CreateText(backingFile))
                {
                    await writer.WriteLineAsync("Updated text");
                }

                using (var reader = new StreamReader(backingFile, true))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {

                    }


                    return line;
                }
            }
        } } }

您需要为 Android.Manifest

添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

检查这个答案。也许它会帮助你 Xamarin Forms access denied when writing to file with FileStream

一般来说,处理文件更简单的方法是使用 document 中的那些方法,例如:

   public MainPage()
    {
        InitializeComponent();

        Label l = new Label()
        {
        };
        StackLayout s = new StackLayout();
        s.Children.Add(l);
        Content = new ScrollView { Content = s };

        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");

        File.WriteAllText(backingFile, "updatedText");
        string text = File.ReadAllText(backingFile);

        l.Text = text;
    }

调试您的代码时,您似乎正在更新 sub thread 中的文本,这在 iOSAndroid 中都是不允许的。另外,我认为从文件中读取文本时不需要 while 语句。我修改了你的代码如下,它对我有效:

public MainPage()
{
    InitializeComponent();

    Label l = new Label()
    {
    };
    StackLayout s = new StackLayout();
    s.Children.Add(l);
    Content = new ScrollView { Content = s };

    ReadTextLine();

    async void ReadTextLine()
    {
        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");
        using (var writer = File.CreateText(backingFile))
        {
            await writer.WriteLineAsync("Updated text");
        }

        using (var reader = new StreamReader(backingFile, true))
        {
            string line;

            line = await reader.ReadLineAsync();

            Device.BeginInvokeOnMainThread(() => {
                l.Text = line;
            });
        }
    }
}

我没有添加权限,它适用于 Android Q 模拟器和 iOS 13 模拟器。您可以尝试添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

AndroidManifest.xml 请求在 Android 项目中的权限,如 Юра Кириченко 在他的回答中提到的那样。