10

I'm building a Windows 8 metro app with XAML/C#. I'm saving an .xml file my data structure with a stream, like this:

XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));

using (var stream = await App.LocalStorage.OpenStreamForWriteAsync(MyObject.Title + ".xml", Windows.Storage.CreationCollisionOption.GenerateUniqueName))
    serializer.Serialize(stream, MyObject);

Where:

App.LocalStorage

Is obviously a StorageFolder objecty set to

Windows.Storage.ApplicationData.Current.LocalFolder

The GenerateUniqueName option is set in order to avoid collisions, because my objects can have the same title. Now, I need to get the file name my stream generated, how can I get it?

Thank you

Brian
  • 5,069
  • 7
  • 37
  • 47
Federinik
  • 523
  • 4
  • 20
  • 3
    did you already try debugging and checking all properties of the stream object? the concrete class and all available fields? I believe base stream or something like that should be there, had similar issue in the past. – Davide Piras Apr 23 '13 at 15:59
  • What is MyObject and how is the MyObject.Title property set? – Abbas Apr 23 '13 at 16:00

2 Answers2

10

Try creating the file first.

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);
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
2

The OpenStreamForWriteAsync method does not seem to give you any easy way to access this information. You could switch to accessing it another way:

StorageFile file = await App.LocalStorage.CreateFileAsync(...);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    // do stuff, file name is at file.Name
Tim S.
  • 55,448
  • 7
  • 96
  • 122