FileUtilities.cs 1.4 KB
Newer Older
1 2 3 4 5 6 7 8
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
9
using System.Reflection;
10 11 12

namespace Microsoft.DotNet.Build.Tasks
{
13
    internal static partial class FileUtilities
14
    {
15 16 17 18
        private static readonly HashSet<string> s_assemblyExtensions = new HashSet<string>(
            new[] { ".dll", ".exe", ".winmd" },
            StringComparer.OrdinalIgnoreCase);

19 20 21 22 23 24 25 26 27 28 29 30
        public static Version GetFileVersion(string sourcePath)
        {
            var fvi = FileVersionInfo.GetVersionInfo(sourcePath);

            if (fvi != null)
            {
                return new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart, fvi.FilePrivatePart);
            }

            return null;
        }

31
        public static AssemblyName GetAssemblyName(string path)
32
        {
33 34 35 36
            if (!s_assemblyExtensions.Contains(Path.GetExtension(path)))
            {
                return null;
            }
37

38 39
            try
            {
40
                return AssemblyName.GetAssemblyName(path);
41 42 43
            }
            catch (BadImageFormatException)
            {
44
                // Not a valid assembly.
45 46 47
                return null;
            }
        }
48 49
    }
}