Does Stream have a 4 GB limit in UWP?

0

I need to write a very large file from my UWP application and I tried to do it by using a code similar to the following one:

private async void btnWriteData_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker pick = new FolderPicker();
        pick.FileTypeFilter.Add(".bin");
        StorageFolder folder = await pick.PickSingleFolderAsync();
        StorageFile oFile = await folder.CreateFileAsync("ThisIsMyTest.bin");
        long totBytes = 0;
        using (var oStream = await oFile.OpenStreamForWriteAsync())
        {
            byte[] buffer = new byte[4194304];
            long i = 0;
            Random rnd = new Random();
            while (i <= 8589934592){
                rnd.NextBytes(buffer);
                totBytes += buffer.Length;
                Debug.WriteLine("totBytes: " + totBytes.ToString()+"/8789934592");
                
                await oStream.WriteAsync(buffer,0,(int)buffer.Length);
            }
        }
        Debug.WriteLine("Stop!");
   }

When I click the button associated to this event, I watch the progress of the operation. The destination folder is on a drive with plenty of free space but when totBytes reaches 4.294.967.296 bytes written, the application rises a System.Exception stating that there isn't enough free space (Exception from HRESULT: 0x80070070). This is the stacktrace:

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.IO.BufferedStream.<WriteToUnderlyingStreamAsync>d__62.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MyApp.MainPage.<btnWriteData_Click>d__12.MoveNext() in C:\Users\matte\Documents\projectx\Windows10\MyApp\MainPage.xaml.cs:line 151

Am I doing anything wrong? Is there a way to write a file larger than 4 GB by using streams?

c#
windows
uwp
stream
asked on Stack Overflow Nov 30, 2020 by ZipGenius

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0