Hi @Dani_S , Welcome to Microsoft Q&A,
The using
statement ensures that resources are automatically closed and released after execution is complete, without the need to explicitly call the Dispose()
method. This not only makes the code more concise, but also ensures that resources are properly released even in the event of an exception.
If contentStream.CopyTo(fileStream)
may take a long time to run, consider using an asynchronous method to avoid blocking the thread.
Before creating the file, make sure the destination directory exists.
using System.IO;
using System.Threading.Tasks;
// Ensure the directory exists
Directory.CreateDirectory(innerFolderPath);
var zipPathForFolderOrPathForFile = $"{innerFolderPath}\\{fileNameResults}";
using (var fileStream = File.Create(zipPathForFolderOrPathForFile))
{
await contentStream.CopyToAsync(fileStream);
}
// Close and release contentStream
await contentStream.DisposeAsync();
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.