简体   繁体   English

如何在 UWP C# 中将类列表写入文件

[英]How to write a list of classes to a file in UWP C#

I have attempted to use the methods on this thread by deadlydog: How to quickly save/load class instance to file我曾尝试使用 deadlydog 在此线程上的方法: How to quick save/load class instance to file

This works in a console application for writing a list of classes to a file.这适用于将类列表写入文件的控制台应用程序。 However when I try migrating it to a UWP project I always get "Access to the path ..... is denied"但是,当我尝试将其迁移到 UWP 项目时,我总是收到“对路径的访问..... 被拒绝”

According to this thread: broadFileSystemAccess UWP I am meant to add broadFileSystemAccess capabilities, which I have done.根据这个线程: broadFileSystemAccess UWP我打算添加 BroadFileSystemAccess 功能,我已经完成了。

However, it also says that I need to use StorageFile APIs , not just classic System.IO API .但是,它还说我需要使用StorageFile APIs ,而不仅仅是经典的System.IO API

I believe the current writing method I am using is utilizing the System.IO API but I do not know how to achieve the same goal using StorageFile APIs .我相信我当前使用的编写方法是利用 System.IO API,但我不知道如何使用StorageFile API实现相同的目标。

Could someone please point me in the correct direction?有人可以指出我正确的方向吗?

You can use the System.IO API, just be sure your code doesn't try to access anything outside its StorageFolder.您可以使用 System.IO API,只要确保您的代码不会尝试访问其 StorageFolder 之外的任何内容。 You can get the StorageFolder path at runtime with this code您可以使用此代码在运行时获取 StorageFolder 路径

Windows.ApplicationModel.Package.Current.InstalledLocation.Path

There is also documentation for how to handle IO in UWP here: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files这里还有关于如何在 UWP 中处理 IO 的文档: https : //docs.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files

UWP apps are only allowed to write to a small subset of folders on your machine. UWP 应用程序只允许写入您计算机上的一小部分文件夹。 Using broadFileSystemAccess capability might alleviate it, but it's like using a sledgehammer when you need a screwdriver.使用 BroadFileSystemAccess 功能可能会缓解它,但这就像当您需要螺丝刀时使用大锤。

Your application to write data it needs to Windows.Store.ApplicationData.LocalFolder.Path .您的应用程序将它需要的数据写入Windows.Store.ApplicationData.LocalFolder.Path That folder is writable without any special capabilities.该文件夹是可写的,无需任何特殊功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM