Caliphe
Caliphe

Reputation: 25

Allocate disk space for a StorageFile in UWP

i'm using a Backoungdownloader to download my files and i would like to allocate disk space before the download starts.

Ex : There are 30 Gb remaining on the disk space of my PC and I want to download a file of 29 Gb. If I could allocate memory, this ensures that my file will be completely downloaded, otherwise I could download another 5Gb file during the download and there will not be enough space for the first file.

So, is there a way to allocate memory like :

StorageFile file = ...;
file.SetLength(...); // Allocation

BackougroundDownloader bd = new BackgroundDownloader();
DownloadOperation do = bd.CreateDownload(url, file);

Thank's !

Upvotes: 1

Views: 177

Answers (1)

Romasz
Romasz

Reputation: 29790

You can use Stream.SetLength for that purpose. Below is a sample of method creating a 1GB file:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var picker = new FileSavePicker() { SuggestedFileName = "test.txt", SuggestedStartLocation = PickerLocationId.Downloads };
    picker.FileTypeChoices.Add("Space", new List<string>() { ".txt" });
    var file = await picker.PickSaveFileAsync();
    using (var stream = await file.OpenStreamForWriteAsync())
    {
        stream.SetLength(1024 * 1024 * 1024);
    }
}

Upvotes: 2

Related Questions