FileUtilities.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Reflection;
  8. namespace RepoTasks;
  9. internal static partial class FileUtilities
  10. {
  11. private static readonly HashSet<string> s_assemblyExtensions = new HashSet<string>(
  12. new[] { ".dll", ".exe", ".winmd" },
  13. StringComparer.OrdinalIgnoreCase);
  14. public static Version GetFileVersion(string sourcePath)
  15. {
  16. var fvi = FileVersionInfo.GetVersionInfo(sourcePath);
  17. if (fvi != null)
  18. {
  19. return new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart, fvi.FilePrivatePart);
  20. }
  21. return null;
  22. }
  23. public static AssemblyName GetAssemblyName(string path)
  24. {
  25. if (!s_assemblyExtensions.Contains(Path.GetExtension(path)))
  26. {
  27. return null;
  28. }
  29. try
  30. {
  31. return AssemblyName.GetAssemblyName(path);
  32. }
  33. catch (BadImageFormatException)
  34. {
  35. // Not a valid assembly.
  36. return null;
  37. }
  38. }
  39. }