System.IO FileNotFoundException: licencia.txt

huangapple go评论61阅读模式
英文:

System.IO FileNotFoundException:licencia.txt

问题

在Net Maui中,我试图读取位于我的Resources\Raw结构中的TXT文件,并将其内容打印在页面上。输出内容被捕获在名为"tvLicense"的标签中,它在License.xaml中声明。

在Net MAUI中,我收到了一个错误,但不知道问题出在哪里。请帮助我吗?

using System.Text;

namespace NautilusSMS.Pages;

public partial class License : ContentPage
{ 
	public License()
	{
		InitializeComponent();
	}

	private async void License_Load(object sender, EventArgs e)
	{
        tvLicense.Text = "";
        StringBuilder sb = new StringBuilder();

        try
        {
            using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync("licencia.txt");
            using StreamReader reader = new StreamReader(fileStream);

            String line = reader.ReadLine();
            sb.Append(line).Append("\n");
            while (line != null)
            {
                line = reader.ReadLine();
                if (line != null)
                {
                    sb.Append(line).Append("\n");
                }
            }
            tvLicense.Text = sb.ToString();
            reader.Close();
        }
        catch (Exception ex)
        {
            DisplayAlert("Alerta", ex.ToString(), "Cancel");
        }//End Catch
    }//End Load

}//End Class
英文:

/* In Net Maui I am trying to read a TXT file that I have in my Resoruces\Raw structure to print its content on the page. The output is captured in a Label x:Name="tvLicense" declared in License.xaml.
In Net MAUI give me the error: I don't find where. Any help Please? */

using System.Text;

namespace NautilusSMS.Pages;

public partial class License : ContentPage
{ 
	public License()
	{
		InitializeComponent();
	}

	private async void License_Load(object sender, EventArgs e)
	{
        tvLicense.Text = "";
        StringBuilder sb = new StringBuilder();

        try
        {
            using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync("licencia.txt");
            using StreamReader reader = new StreamReader(fileStream);

            String line = reader.ReadLine();
            sb.Append(line).Append("\n");
            while (line != null)
            {
                line = reader.ReadLine();
                if (line != null)
                {
                    sb.Append(line).Append("\n");
                }
            }
            tvLicense.Text = sb.ToString();
            reader.Close();
        }
        catch (Exception ex)
        {
            DisplayAlert("Alerta", ex.ToString(), "Cancel");
        }//End Catch
    }//End Load

}//End Class

答案1

得分: 1

我测试了你提供的代码。报告了这个错误(System.IO FileNotFoundException),因为你没有将你的.txt文件设置为MauiAsset

FileSystem.OpenAppPackageFileAsync

使用Build ActionMauiAsset的项目中添加的文件可以使用这种方法打开。 .NET MAUI 项目将处理Resources\Raw文件夹中的任何文件作为MauiAsset

欲了解更多信息,请参考文件系统助手官方文档。

更新:

右键单击licencia.txt文件,然后单击属性 -> 将Build Action设置为MauiAsset

英文:

I tested the code you provided. This error(System.IO FileNotFoundException) was reported because you didn't set your .txt file as a MauiAsset.

FileSystem.OpenAppPackageFileAsync

>Files that were added to the project with the Build Action of MauiAsset can be opened with this method. .NET MAUI projects will process any file in the Resources\Raw folder as a MauiAsset.

For more information, you can refer to File system helpers by official.

Update:

Right-click the licencia.txt file, and then click Properties -> set Build Action to MauiAsset.

huangapple
  • 本文由 发表于 2023年3月1日 13:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定