Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Convert into await using statement or declaration #16694

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Files.App/Extensions/ImageSourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static async Task<byte[]> ToByteArrayAsync(this IInputStream stream)
return null;
}

using var readStream = stream.AsStreamForRead();
await using var readStream = stream.AsStreamForRead();

return await readStream.ToByteArrayAsync();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Services/App/AppUpdateSideloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public async Task CheckAndUpdateFilesLauncherAsync()

if (File.Exists(destHashFilePath))
{
using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
using var destStream = File.OpenRead(destHashFilePath);
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
await using var destStream = File.OpenRead(destHashFilePath);

hashEqual = HashEqual(srcStream, destStream);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Services/App/AppUpdateStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public async Task CheckAndUpdateFilesLauncherAsync()

if (Path.Exists(destHashFilePath))
{
using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
using var destStream = File.OpenRead(destHashFilePath);
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
await using var destStream = File.OpenRead(destHashFilePath);

hashEqual = HashEqual(srcStream, destStream);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a

// Build the connection and sql command
SQLitePCL.Batteries_V2.Init();
using var database = new SqliteConnection($"Data Source='{syncDbPath}'");
using var cmdRoot = new SqliteCommand("SELECT * FROM roots", database);
using var cmdMedia = new SqliteCommand("SELECT * FROM media WHERE fs_type=10", database);
await using var database = new SqliteConnection($"Data Source='{syncDbPath}'");
await using var cmdRoot = new SqliteCommand("SELECT * FROM roots", database);
await using var cmdMedia = new SqliteCommand("SELECT * FROM media WHERE fs_type=10", database);

// Open the connection and execute the command
database.Open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ protected override async IAsyncEnumerable<ICloudProvider> GetProviders()

// Build the connection and SQL command
SQLitePCL.Batteries_V2.Init();
using var database = new SqliteConnection($"Data Source='{syncDbPath}'");
using var cmdConnection = new SqliteCommand("SELECT * FROM connection_table", database);
using var cmdTable = new SqliteCommand("SELECT * FROM session_table", database);
await using var database = new SqliteConnection($"Data Source='{syncDbPath}'");
await using var cmdConnection = new SqliteCommand("SELECT * FROM connection_table", database);
await using var cmdTable = new SqliteCommand("SELECT * FROM session_table", database);

// Open the connection and execute the command
database.Open();
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Utils/Shell/LaunchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ private static async Task<bool> HandleApplicationLaunch(string application, stri
if (!hFileSrc.IsInvalid && !hFileDst.IsInvalid)
{
// Copy ADS to temp folder and open
using (var inStream = new FileStream(hFileSrc.DangerousGetHandle(), FileAccess.Read))
using (var outStream = new FileStream(hFileDst.DangerousGetHandle(), FileAccess.Write))
await using (var inStream = new FileStream(hFileSrc.DangerousGetHandle(), FileAccess.Read))
await using (var outStream = new FileStream(hFileDst.DangerousGetHandle(), FileAccess.Write))
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IList<IStorageItemWithPath> so

var operationID = Guid.NewGuid().ToString();

using var r = cancellationToken.Register(CancelOperation, operationID, false);
await using var r = cancellationToken.Register(CancelOperation, operationID, false);

var sourceReplace = sourceNoSkip.Zip(collisionsNoSkip, (src, coll) => new { src, coll }).Where(item => item.coll == FileNameConflictResolveOptionType.ReplaceExisting).Select(item => item.src);
var destinationReplace = destinationNoSkip.Zip(collisionsNoSkip, (src, coll) => new { src, coll }).Where(item => item.coll == FileNameConflictResolveOptionType.ReplaceExisting).Select(item => item.src);
Expand Down Expand Up @@ -371,7 +371,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IList<IStorageItemWithPath>
}

var operationID = Guid.NewGuid().ToString();
using var r = cancellationToken.Register(CancelOperation, operationID, false);
await using var r = cancellationToken.Register(CancelOperation, operationID, false);

var (success, response) = await FileOperationsHelpers.DeleteItemAsync(deleteFilePaths.ToArray(), permanently, MainWindow.Instance.WindowHandle.ToInt64(), asAdmin, progress, operationID);

Expand Down Expand Up @@ -483,7 +483,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IList<IStorageItemWithPath> so
var collisionsNoSkip = collisions.Where(c => c != FileNameConflictResolveOptionType.Skip);
var operationID = Guid.NewGuid().ToString();

using var r = cancellationToken.Register(CancelOperation, operationID, false);
await using var r = cancellationToken.Register(CancelOperation, operationID, false);

var sourceReplace = sourceNoSkip.Zip(collisionsNoSkip, (src, coll) => new { src, coll }).Where(item => item.coll == FileNameConflictResolveOptionType.ReplaceExisting).Select(item => item.src);
var destinationReplace = destinationNoSkip.Zip(collisionsNoSkip, (src, coll) => new { src, coll }).Where(item => item.coll == FileNameConflictResolveOptionType.ReplaceExisting).Select(item => item.src);
Expand Down Expand Up @@ -736,7 +736,7 @@ public async Task<IStorageHistory> RestoreItemsFromTrashAsync(IList<IStorageItem
StatusCenterItemProgressModel fsProgress = new(progress, true, FileSystemStatusCode.InProgress);
fsProgress.Report();
var operationID = Guid.NewGuid().ToString();
using var r = cancellationToken.Register(CancelOperation, operationID, false);
await using var r = cancellationToken.Register(CancelOperation, operationID, false);

var moveResult = new ShellOperationResult();
var (status, response) = await FileOperationsHelpers.MoveItemAsync(source.Select(s => s.Path).ToArray(), [.. destination], false, MainWindow.Instance.WindowHandle.ToInt64(), asAdmin, progress, operationID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static IAsyncOperation<BaseStorageFile> GetFileFromPathAsync(string path)
public async Task<string> ReadTextAsync(int maxLength = -1)
{
using var inputStream = await OpenReadAsync();
using var stream = inputStream.AsStreamForRead();
await using var stream = inputStream.AsStreamForRead();
using var dataReader = new StreamReader(stream, true);
StringBuilder builder = new();
int charsRead, charsToRead;
Expand Down
6 changes: 3 additions & 3 deletions src/Files.App/Utils/Storage/StorageItems/FtpStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin

if (destFolder is ICreateFileWithStream cwsf)
{
using var inStream = await ftpClient.OpenRead(FtpPath, token: cancellationToken);
await using var inStream = await ftpClient.OpenRead(FtpPath, token: cancellationToken);
return await cwsf.CreateFileAsync(inStream, desiredNewName, option.Convert());
}
else
{
BaseStorageFile file = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
using var stream = await file.OpenStreamForWriteAsync();
await using var stream = await file.OpenStreamForWriteAsync();
return await ftpClient.DownloadStream(stream, FtpPath, token: cancellationToken) ? file : null;
}
}, ((IPasswordProtectedItem)this).RetryWithCredentialsAsync));
Expand Down Expand Up @@ -279,7 +279,7 @@ private async void FtpDataStreamingHandlerAsync(StreamedFileDataRequest request)
return;
}

using (var outStream = request.AsStreamForWrite())
await using (var outStream = request.AsStreamForWrite())
{
await ftpClient.DownloadStream(outStream, FtpPath);
await outStream.FlushAsync();
Expand Down
8 changes: 4 additions & 4 deletions src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
else
{
destFile.CreateFile();
using (var inStream = await this.OpenStreamForReadAsync())
using (var outStream = await destFile.OpenStreamForWriteAsync())
await using (var inStream = await this.OpenStreamForReadAsync())
await using (var outStream = await destFile.OpenStreamForWriteAsync())
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down Expand Up @@ -248,8 +248,8 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
else
{
destFile.CreateFile();
using (var inStream = await this.OpenStreamForReadAsync())
using (var outStream = await destFile.OpenStreamForWriteAsync())
await using (var inStream = await this.OpenStreamForReadAsync())
await using (var outStream = await destFile.OpenStreamForWriteAsync())
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down
18 changes: 9 additions & 9 deletions src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
}
else if (destFolder is ICreateFileWithStream cwsf)
{
using var inStream = await this.OpenStreamForReadAsync();
await using var inStream = await this.OpenStreamForReadAsync();
return await cwsf.CreateFileAsync(inStream, desiredNewName, option.Convert());
}
else
{
var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
using (var inStream = await this.OpenStreamForReadAsync())
using (var outStream = await destFile.OpenStreamForWriteAsync())
await using (var inStream = await this.OpenStreamForReadAsync())
await using (var outStream = await destFile.OpenStreamForWriteAsync())
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand All @@ -93,8 +93,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
option == NameCollisionOption.ReplaceExisting);
if (!hFile.IsInvalid)
{
using (var inStream = await this.OpenStreamForReadAsync())
using (var outStream = new FileStream(hFile, FileAccess.Write))
await using (var inStream = await this.OpenStreamForReadAsync())
await using (var outStream = new FileStream(hFile, FileAccess.Write))
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down Expand Up @@ -140,8 +140,8 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace)
{
return AsyncInfo.Run(async (cancellationToken) =>
{
using var inStream = await this.OpenStreamForReadAsync();
using var outStream = await fileToReplace.OpenStreamForWriteAsync();
await using var inStream = await this.OpenStreamForReadAsync();
await using var outStream = await fileToReplace.OpenStreamForWriteAsync();

await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand All @@ -151,8 +151,8 @@ public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace)
{
return AsyncInfo.Run(async (cancellationToken) =>
{
using var inStream = await this.OpenStreamForReadAsync();
using var outStream = await fileToReplace.OpenStreamForWriteAsync();
await using var inStream = await this.OpenStreamForReadAsync();
await using var outStream = await fileToReplace.OpenStreamForWriteAsync();

await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private async void StreamedFileWriterAsync(StreamedFileDataRequest request)
{
try
{
using (var stream = request.AsStreamForWrite())
await using (var stream = request.AsStreamForWrite())
{
await Contents.CopyToAsync(stream);
await stream.FlushAsync();
Expand Down Expand Up @@ -119,14 +119,14 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin

if (destFolder is ICreateFileWithStream cwsf)
{
using var inStream = await this.OpenStreamForReadAsync();
await using var inStream = await this.OpenStreamForReadAsync();
return await cwsf.CreateFileAsync(inStream, desiredNewName, option.Convert());
}
else
{
var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
using (var inStream = await this.OpenStreamForReadAsync())
using (var outStream = await destFile.OpenStreamForWriteAsync())
await using (var inStream = await this.OpenStreamForReadAsync())
await using (var outStream = await destFile.OpenStreamForWriteAsync())
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private async void StreamedFileWriterAsync(StreamedFileDataRequest request)
{
try
{
using (var stream = request.AsStreamForWrite())
await using (var stream = request.AsStreamForWrite())
{
await stream.FlushAsync();
}
Expand Down
15 changes: 8 additions & 7 deletions src/Files.App/Utils/Storage/StorageItems/ZipStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
else
{
var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
using var outStream = await destFile.OpenStreamForWriteAsync();
await using var outStream = await destFile.OpenStreamForWriteAsync();
await SafetyExtensions.WrapAsync(() => zipFile.ExtractFileAsync(entry.Index, outStream), async (_, exception) =>
{
await destFile.DeleteAsync();
Expand All @@ -281,7 +281,7 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace)
}

using var hDestFile = fileToReplace.CreateSafeFileHandle(FileAccess.ReadWrite);
using (var outStream = new FileStream(hDestFile, FileAccess.Write))
await using (var outStream = new FileStream(hDestFile, FileAccess.Write))
{
await zipFile.ExtractFileAsync(entry.Index, outStream);
}
Expand Down Expand Up @@ -323,14 +323,15 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
}
using (var ms = new MemoryStream())
{
using (var archiveStream = await OpenZipFileAsync(FileAccessMode.Read))
await using (var archiveStream = await OpenZipFileAsync(FileAccessMode.Read))
{
SevenZipCompressor compressor = new SevenZipCompressor() { CompressionMode = CompressionMode.Append };
compressor.SetFormatFromExistingArchive(archiveStream);
var fileName = IO.Path.GetRelativePath(containerPath, IO.Path.Combine(IO.Path.GetDirectoryName(Path), desiredName));
await compressor.ModifyArchiveAsync(archiveStream, new Dictionary<int, string>() { { index, fileName } }, Credentials.Password, ms);
}
using (var archiveStream = await OpenZipFileAsync(FileAccessMode.ReadWrite))

await using (var archiveStream = await OpenZipFileAsync(FileAccessMode.ReadWrite))
{
ms.Position = 0;
await ms.CopyToAsync(archiveStream);
Expand Down Expand Up @@ -371,13 +372,13 @@ public override IAsyncAction DeleteAsync(StorageDeleteOption option)
}
using (var ms = new MemoryStream())
{
using (var archiveStream = await OpenZipFileAsync(FileAccessMode.Read))
await using (var archiveStream = await OpenZipFileAsync(FileAccessMode.Read))
{
SevenZipCompressor compressor = new SevenZipCompressor() { CompressionMode = CompressionMode.Append };
compressor.SetFormatFromExistingArchive(archiveStream);
await compressor.ModifyArchiveAsync(archiveStream, new Dictionary<int, string>() { { index, null } }, Credentials.Password, ms);
}
using (var archiveStream = await OpenZipFileAsync(FileAccessMode.ReadWrite))
await using (var archiveStream = await OpenZipFileAsync(FileAccessMode.ReadWrite))
{
ms.Position = 0;
await ms.CopyToAsync(archiveStream);
Expand Down Expand Up @@ -505,7 +506,7 @@ private StreamedFileDataRequestedHandler ZipDataStreamingHandler(string name)
}
else
{
using (var outStream = request.AsStreamForWrite())
await using (var outStream = request.AsStreamForWrite())
{
await zipFile.ExtractFileAsync(entry.Index, outStream);
}
Expand Down
Loading