我正在使用XAML / C#构建Windows 8 metro应用程序。我正在使用流保存.xml文件我的数据结构,如下所示:
XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));
using (var stream = await App.LocalStorage.OpenStreamForWriteAsync(MyObject.Title + ".xml", Windows.Storage.CreationCollisionOption.GenerateUniqueName))
serializer.Serialize(stream, MyObject);
其中:
App.LocalStorage
显然,StorageFolder对象设置为
Windows.Storage.ApplicationData.Current.LocalFolder
设置GenerateUniqueName选项是为了避免冲突,因为我的对象可以具有相同的标题。现在,我需要获取生成流的文件名,我该如何获取它?
谢谢
答案 0 :(得分:10)
首先尝试创建文件。
var sourceFileName = MyObject.Title + ".xml";
StorageFile storageFile = await App.LocalStorage.CreateFileAsync(sourceFileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
using (var stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
serializer.Serialize(stream, MyObject);
}
答案 1 :(得分:2)
OpenStreamForWriteAsync
方法似乎没有为您提供访问此信息的简便方法。您可以切换到另一种方式访问它:
StorageFile file = await App.LocalStorage.CreateFileAsync(...);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
// do stuff, file name is at file.Name