كيف يمكنني الحصول على نسخة من تجميع دون تحميل ذلك؟

StackOverflow https://stackoverflow.com/questions/187999

  •  06-07-2019
  •  | 
  •  

سؤال

وظيفة واحدة صغيرة من برنامج واسع يتناول التجميعات في مجلد واستبدال خارج تاريخ المجالس مع أحدث الإصدارات. ولتحقيق ذلك، فإنه يحتاج إلى قراءة أرقام إصدارات الملفات التجميع الحالية دون تحميل فعلا تلك المجالس في عملية التنفيذ.

هل كانت مفيدة؟

المحلول

ولقد وجدت ما يلي <لأ href = "http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-get-assembly-version-without-loading-it.aspx" يختلط = "noreferrer"> في هذه المقالة .

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's nothing to update
    return;
}

// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);

نصائح أخرى

واعتمادا على الملفات، يمكن FileVersionInfo خيار واحد - أي بمعنى

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path)
string ver = fvi.FileVersion;

والمشكلة هي أن هذا يعتمد على رمز وجود السمة [AssemblyFileVersion]، وأنها مطابقة السمة [AssemblyVersion].

وأنا أعتقد أن ننظر في خيارات AssemblyName اقترح من قبل الآخرين أولا، وإن كان.

استخدم AssemblyName.GetAssemblyName("assembly.dll");، ثم تحليل الاسم. ووفقا ل MSDN :

<اقتباس فقرة>   

وهذا سوف يعمل فقط إذا كان الملف   يحتوي على بيان الجمعية. هذه   يتسبب أسلوب الملف ليتم فتحه   ومغلقة، ولكن الجمعية لا   وأضاف في هذا المجال.

وفقط للسجل: هنا كيفية الحصول على إصدار الملف في C # .NET Framework ضغط. انها في الاساس من OpenNETCF لكن أقصر جدا وexctacted بحيث يمكن من خلال copy'n'pasted. أمل أنها سوف تساعد ...

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top