3

我在尝试将文件保存到我正在使用的 WinRT 应用程序的文件夹中时遇到问题。更具体地说:我无法检索文件夹。我需要保存一些文件,然后从同一个文件夹中检索它们,假设MyFolder它实际上在应用程序中与Assets文件夹处于同一级别。我怎样才能做到这一点?

到目前为止我试过

await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("MyFolder")

await ApplicationData.Current.LocalFolder.GetFolderAsync("MyFolder");

两者似乎都告诉我这样的文件夹不存在。现在,我似乎无法弄清楚我是否在正确的轨道上,或者这是我所做的愚蠢的事情并导致了那个异常。我不能真正发布整个代码,因为它实际上是一个更大的方法与其他调用的调用,等等。

另外,我想知道(如果这些方法中的任何一个是正确的),如果应用程序获得更新,内容会受到任何影响(更好地说,丢失)。

谢谢你。

4

2 回答 2

10

Windows.ApplicationModel.Package.Current.InstalledLocation will give you the 'file system' defined by the application package, so you'd see you the commonly used Asset folder, for instance, via the URI ms-appx:///Assets Anything accessed via InstalledLocation or the ms-appx scheme is read-only because it's defined by the project you deployed from Visual Studio.

ApplicationData.Current.LocalFolder allows you to access the 'file system' owned by the current application/user (and stored at %AppData%/Local/Packages/<yourapppackage>/LocalState). You can create whatever structure you want here to manage your various roles, and use the ms-appdata:///local/DirLeve1/DirLeve2/File3 URI to access your files). That storage is retained through updates to your application, though, of course, you'll need a strategy for updating whatever stored data formats are required to interact with the newer version of your application.

It sounds like you're assuming LocalFolder cannot contain a folder hiearachy; it can, and so you could have a Recents subfolder for instance under the LocalFolder reference and handle things that way.

于 2013-08-10T01:40:07.143 回答
3

我认为您需要使用ms-appxURI。像这样的东西:

await StorageFolder.GetFolderFromPathAsync("ms-appx:///MyFolder");

此外,您需要确保该文件夹实际上包含在输出中。我不确定你是如何在 C# 项目中做到这一点的——在 C++ 中,它是通过将文件夹/文件的“Is Content”构建属性设置为 true 来实现的。

于 2013-08-10T00:45:43.003 回答