英文:
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 Action为MauiAsset的项目中添加的文件可以使用这种方法打开。 .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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论