Search code examples
c#.netiowindows-store-appswindows-rt

Clear file content with FileSavePicker in Windows store app / RT


How can I clear the contents of the file before filling in new content / overwrite the old file? If the new content is shorter than the old one, remaining parts of the old content destroys my datacontract structure.

FileSavePicker savePicker = new FileSavePicker();
StorageFile saveFile = await savePicker.PickSaveFileAsync();

using (var sessionRandomAccess = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
{
    using (var sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0))
    {
        var ser = new DataContractSerializer(typeof(ConfigurationModel));
        ser.WriteObject(sessionOutputStream.AsStreamForWrite(), model);
    }
}

Solution

  • As always, after posting a questing i figured it out myself and it looks like i spend no time in research.

    Clears the file before writing new content:

    await FileIO.WriteTextAsync(saveFile, string.Empty);