Одна небольшая функция большой программы проверяет сборки в папке и заменяет устаревшие сборки с последними версиями. Для этого ему необходимо прочитать номера версий существующих файлов сборки, не загружая эти сборки в исполняемый процесс.
Как получить версию сборки без ее загрузки?
Ответ 1
В этой статье я нашел .
using System.Reflection;
using System.IO;
...
// Get current and updated assemblies
AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);
// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
// There nothing to update
return;
}
// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);
Ответ 2
В зависимости от файлов один параметр может быть FileVersionInfo
- i.e.
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path)
string ver = fvi.FileVersion;
Проблема в том, что это зависит от кода, имеющего атрибут [AssemblyFileVersion]
, и он соответствует атрибуту [AssemblyVersion]
.
Я думаю, что сначала посмотрю варианты AssemblyName, предложенные другими.
Ответ 3
Используйте AssemblyName.GetAssemblyName("assembly.dll");
, затем проанализируйте имя. Согласно MSDN:
Это будет работать, только если файл содержит манифест сборки. Эта метод вызывает открытие файла и закрыт, но сборка не добавлен в этот домен.
Ответ 4
Только для записи: вот как получить версию файла в С#.NET Compact Framework. Это в основном из OpenNETCF, но довольно короче и exctacted, поэтому он может копироваться. Надеюсь, это поможет...
public static Version GetFileVersionCe(string fileName)
{
int handle = 0;
int length = GetFileVersionInfoSize(fileName, ref handle);
Version v = null;
if (length > 0)
{
IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(length);
if (GetFileVersionInfo(fileName, handle, length, buffer))
{
IntPtr fixedbuffer = IntPtr.Zero;
int fixedlen = 0;
if (VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
{
byte[] fixedversioninfo = new byte[fixedlen];
System.Runtime.InteropServices.Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen);
v = new Version(
BitConverter.ToInt16(fixedversioninfo, 10),
BitConverter.ToInt16(fixedversioninfo, 8),
BitConverter.ToInt16(fixedversioninfo, 14),
BitConverter.ToInt16(fixedversioninfo, 12));
}
}
Marshal.FreeHGlobal(buffer);
}
return v;
}
[DllImport("coredll", EntryPoint = "GetFileVersionInfo", SetLastError = true)]
private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);
[DllImport("coredll", EntryPoint = "GetFileVersionInfoSize", SetLastError = true)]
private static extern int GetFileVersionInfoSize(string filename, ref int handle);
[DllImport("coredll", EntryPoint = "VerQueryValue", SetLastError = true)]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);