FileUtilities.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Reflection;
  8. namespace RepoTasks
  9. {
  10. internal static partial class FileUtilities
  11. {
  12. private static readonly HashSet<string> s_assemblyExtensions = new HashSet<string>(
  13. new[] { ".dll", ".exe", ".winmd" },
  14. StringComparer.OrdinalIgnoreCase);
  15. public static Version GetFileVersion(string sourcePath)
  16. {
  17. var fvi = FileVersionInfo.GetVersionInfo(sourcePath);
  18. if (fvi != null)
  19. {
  20. return new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart, fvi.FilePrivatePart);
  21. }
  22. return null;
  23. }
  24. public static AssemblyName GetAssemblyName(string path)
  25. {
  26. if (!s_assemblyExtensions.Contains(Path.GetExtension(path)))
  27. {
  28. return null;
  29. }
  30. try
  31. {
  32. return AssemblyName.GetAssemblyName(path);
  33. }
  34. catch (BadImageFormatException)
  35. {
  36. // Not a valid assembly.
  37. return null;
  38. }
  39. }
  40. }
  41. }