diff --git a/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj b/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj
index 6fabdcd1f6..465496ad06 100644
--- a/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj
+++ b/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj
@@ -34,7 +34,6 @@
-
diff --git a/src/Microsoft.DotNet.Build.Tasks/NativeMethods.cs b/src/Microsoft.DotNet.Build.Tasks/NativeMethods.cs
deleted file mode 100644
index b1bffb5914..0000000000
--- a/src/Microsoft.DotNet.Build.Tasks/NativeMethods.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright (c) Microsoft. All rights reserved.
-// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-
-using System.Runtime.InteropServices;
-using System.Text;
-
-namespace Microsoft.DotNet.Build.Tasks
-{
- internal static class NativeMethods
- {
- internal const uint ERROR_INSUFFICIENT_BUFFER = 0x8007007A;
- internal const uint S_OK = 0x0;
-
- ///
- /// Get the runtime version for a given file
- ///
- /// The path of the file to be examined
- /// The buffer allocated for the version information that is returned.
- /// The size, in wide characters, of szBuffer
- /// The size, in bytes, of the returned szBuffer.
- /// HResult
- [DllImport("mscoree.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- internal static extern uint GetFileVersion(string szFullPath, StringBuilder szBuffer, int cchBuffer, out uint dwLength);
- }
-}
diff --git a/src/Microsoft.DotNet.Build.Tasks/PrereleaseResolveNuGetPackageAssets.cs b/src/Microsoft.DotNet.Build.Tasks/PrereleaseResolveNuGetPackageAssets.cs
index 5526b488c2..1146cccae3 100644
--- a/src/Microsoft.DotNet.Build.Tasks/PrereleaseResolveNuGetPackageAssets.cs
+++ b/src/Microsoft.DotNet.Build.Tasks/PrereleaseResolveNuGetPackageAssets.cs
@@ -7,6 +7,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Reflection.Metadata;
+using System.Reflection.PortableExecutable;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
@@ -67,7 +69,6 @@ internal PrereleaseResolveNuGetPackageAssets(DirectoryExists directoryExists, Fi
///
public PrereleaseResolveNuGetPackageAssets()
{
- Log.TaskResources = Strings.ResourceManager;
}
///
@@ -156,6 +157,8 @@ public bool OmitTransitiveCompileReferences
///
public override bool Execute()
{
+ Log.TaskResources = Strings.ResourceManager;
+
try
{
ExecuteCore();
@@ -664,26 +667,17 @@ private void TryParseRuntimeVersion(string imageRuntimeVersion, out bool isWinMD
/// The CLR runtime version or empty if the path does not exist.
private static string TryGetRuntimeVersion(string path)
{
- StringBuilder runtimeVersion = null;
- uint hresult = 0;
- uint actualBufferSize = 0;
- int bufferLength = 11; // 11 is the length of a runtime version and null terminator v2.0.50727/0
-
- do
- {
- runtimeVersion = new StringBuilder(bufferLength);
- hresult = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out actualBufferSize);
- bufferLength = bufferLength * 2;
-
- } while (hresult == NativeMethods.ERROR_INSUFFICIENT_BUFFER);
-
- if (hresult == NativeMethods.S_OK && runtimeVersion != null)
+ try
{
- return runtimeVersion.ToString();
+ using (FileStream stream = File.OpenRead(path))
+ using (PEReader peReader = new PEReader(stream))
+ {
+ return peReader.GetMetadataReader().MetadataVersion;
+ }
}
- else
+ catch (Exception)
{
- return String.Empty;
+ return string.Empty;
}
}
}