From fa02e84d665b0f02a2b8dd8a8e298034268ceff3 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 6 Jul 2017 23:58:23 -0600 Subject: [PATCH] Close FileStream opened by Get-FileHash (#4175) Fix #4174 --- .../commands/utility/GetHash.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs index d55c983913..336c4829d4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs @@ -121,10 +121,11 @@ namespace Microsoft.PowerShell.Commands { byte[] bytehash = null; String hash = null; + Stream openfilestream = null; try { - Stream openfilestream = File.OpenRead(path); + openfilestream = File.OpenRead(path); bytehash = hasher.ComputeHash(openfilestream); hash = BitConverter.ToString(bytehash).Replace("-",""); @@ -138,6 +139,10 @@ namespace Microsoft.PowerShell.Commands path); WriteError(errorRecord); } + finally + { + openfilestream?.Dispose(); + } } }