我正在用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选项是为了避免冲突,因为我的对象可以有相同的标题。现在,我需要获取流生成的文件名,我该如何获取呢?
谢谢
尝试先创建文件。
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);
}
OpenStreamForWriteAsync
方法似乎没有给您任何简单的方法来访问此信息。你可以切换到另一种访问方式:
StorageFile file = await App.LocalStorage.CreateFileAsync(...);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
// do stuff, file name is at file.Name