when I used FileSavePicker to select (or create) storage file, if I select existing file, it will prompt for replace. when I press yes, I expect the file to be replaced later but it doesn't.
// existing file is picked, I pressed yes when replace was requested.
file = await picker.PickSaveFileAsync();
file
await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders)
file
var file = await (await file.GetParentAsync()).CreateFileAsync(file.Name,
CreationCollisionOption.ReplaceExisting);
var picker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.Desktop,
DefaultFileExtension = ".bin",
SuggestedFileName = "Binary file"
};
picker.FileTypeChoices.Add("Binary File", new List<string> {".bin"});
// in second run, choose existing file.
var file = await picker.PickSaveFileAsync();
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders))
{
using (var output = stream.GetOutputStreamAt(0))
{
using (var writer = new DataWriter(output))
{
writer.WriteBytes(Enumerable.Repeat<byte>(10, 10000).ToArray());
await writer.StoreAsync();
writer.DetachStream();
}
await output.FlushAsync();
}
}
Repeat<byte>(10, 10000)
Repeat<byte>(80, 50)
You can always SetLength of your opened stream to 0 first - which will clear all the old content and set stream's position to 0. Afterwards you can just write to the stream your new content:
var picker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.Desktop,
DefaultFileExtension = ".bin",
SuggestedFileName = "Binary file"
};
picker.FileTypeChoices.Add("Binary File", new List<string> { ".bin" });
var file = await picker.PickSaveFileAsync();
using (var stream = await file.OpenStreamForWriteAsync())
{
stream.SetLength(0);
var bytes = Encoding.ASCII.GetBytes("Content");
await stream.WriteAsync(bytes, 0, bytes.Length);
await stream.FlushAsync();
}