forked from KirillOsenkov/MetadataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetAssemblyVersion.cs
More file actions
38 lines (36 loc) · 1.17 KB
/
GetAssemblyVersion.cs
File metadata and controls
38 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
namespace PEFile
{
public static class FileUtilities
{
public static Version GetAssemblyVersion(string sourcePath)
{
using (var assemblyStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read))
{
Version result = null;
try
{
using (PEReader peReader = new PEReader(assemblyStream, PEStreamOptions.LeaveOpen))
{
if (peReader.HasMetadata)
{
MetadataReader reader = peReader.GetMetadataReader();
if (reader.IsAssembly)
{
result = reader.GetAssemblyDefinition().Version;
}
}
}
}
catch (BadImageFormatException)
{
// not a PE
}
return result;
}
}
}
}