文件写在.net maui中
我是第一次尝试.NET MAUI,并且正在开发一个离线应用程序,该应用程序只是存储一些将显示在卡片上的信息。关键是要完全离线,以至于不必依靠互联网或数据速度。
我遇到的问题是我无法写入文件,但是我可以从中读取。查看文档我认为它的作用是将文件写入文件的类似方法。
static public async Task WriteTextFile(string filePath,string data)
{
using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath);
using StreamWriter writer = new StreamWriter(fileStream);
await writer.WriteAsync(data);
}
我将其放入我创建的静态UTILS类中,当我放入路径时(例如:“ Cardinfo.json”),该路径存储在资源\ raw \ path中并标记为MauiasSet。
关于为什么我会得到流的不正确例外的任何想法吗?我目前正在Windows上运行它,但是我假设这会延续到Android等。我认为也许这与没有写入功能的流有关,但是OpenAppPackageFileassync没有一个超负荷的方法可以让我设置任何其他属性。
如果我需要更清楚的事情,请告诉我。
I am trying out .NET Maui for the first time and I am working on an offline app that just stores some information that will be displayed on cards. The point is to be completely offline as to not have to rely on internet or data speeds.
The issue I am having is I can't write to the file, but I can read from it. Looking at the File System Helper Documentation I figured it worked a similar way for writing to files.
static public async Task WriteTextFile(string filePath,string data)
{
using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath);
using StreamWriter writer = new StreamWriter(fileStream);
await writer.WriteAsync(data);
}
I put this into a Static Utils class I created and when I put in a path (Ex: "CardInfo.json") which is stored in the Resources\Raw\ path and labeled as a MauiAsset.
Any idea on why I am getting the Stream not Writable Exception? I am running it on Windows at the moment, but I am assuming this will carry over into Android, etc. I thought maybe it had something to do with the stream not having write capability, but the OpenAppPAckageFileAsync doesn't have an overloaded method that could allow for me to set any additional properties.
If I need to make anything more clear in this let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
apppackage
文件已阅读。您可能需要使用
cachedirectory
或appDatadirectory
。The
AppPackage
files are readonly.You may want to use the
CacheDirectory
or theAppDataDirectory
.