2011-03-02 22 views

回答

6

您可以使用以下方法,但前提是您可以保證從「入口」程序集調用它。

public static void GetProductAndVersionEasy(out string productName, out Version productVersion) 
{ 
    var callingAssembly = Assembly.GetCallingAssembly(); 

    // Get the product name from the AssemblyProductAttribute. 
    // Usually defined in AssemblyInfo.cs as: [assembly: AssemblyProduct("Hello World Product")] 
    var assemblyProductAttribute = ((AssemblyProductAttribute[])callingAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute),false)).Single(); 
    productName = assemblyProductAttribute.Product; 

    // Get the product version from the assembly by using its AssemblyName. 
    productVersion = new AssemblyName(callingAssembly.FullName).Version; 
} 

(如果方法在您的輸入程序集中,您可以用GetExecutingAssembly替換GetCallingAssembly)。

我也想出瞭如何從.xap文件中破解這個信息。我將主程序集的字節加載到內存中,然後從最後幾個字節讀取產品和版本信息。我必須寫這個,因爲我需要一個可以在任何地方(即不是執行或調用程序集)調用的基本庫中重用的方法。

public static void GetProductAndVersionHack(
    out string productName, 
    out Version productVersion) 
{ 
    // Get the name of the entry assembly 
    var deployment = System.Windows.Deployment.Current; 
    var entryPointAssembly = deployment.EntryPointAssembly + ".dll"; 

    // Get the assembly stream from the xap file 
    StreamResourceInfo streamResourceInfo = Application.GetResourceStream(
    new Uri(
     entryPointAssembly, 
     UriKind.Relative)); 
    Stream stream = streamResourceInfo.Stream; 

    // The VERSION_INFO struct as at the end of the file. Just read the last 1000 bytes or so from the stream 
    // (Keep in mind that there are a lot of zeroes padded at the end of the stream. You should probably 
    // search for the real stream.Length after trimming them off) 
    stream.Position = stream.Length - 1000; 
    StreamReader streamReader = new StreamReader(stream, Encoding.Unicode); 
    string text = streamReader.ReadToEnd(); 

    // Split the string on the NULL character 
    string[] strings = text.Split(
    new[] { '\0' }, 
    System.StringSplitOptions.RemoveEmptyEntries); 

    // Get the Product Name (starts with unicode character \u0001) 
    int ixProductName = strings.FindIndexOf(line => line.EndsWith("\u0001ProductName")); 
    productName = ixProductName >= 0 ? strings[ixProductName + 1] : null; 

    // Get the Product Version 
    int ixProductVersion = strings.FindIndexOf(line => line.EndsWith("\u0001ProductVersion")); 
    productVersion = ixProductVersion >= 0 ? Version.Parse(strings[ixProductVersion + 1]) : null; 
} 

public static int FindIndexOf<T>(
    this IEnumerable<T> source, 
    Func<T, bool> match) 
{ 
    int i = -1; 
    foreach (var item in source) 
    { 
    ++i; 
    if (match(item)) return i; 
    } 
    return -1; 
} 
+0

糟糕,我現在只注意到我在第二個代碼示例中使用了我的一個擴展方法'FindIndexOf'。我很樂意應要求分享其代碼。 – base2

0

不知道什麼設備你的deving,但對於WP7的Silverlight,在屬性文件夾中包含一個名爲WMAppManifest文件.xml,並且在「部署」>「應用程序」下,它們是名爲Title的字段,您可以將XML從那裏拉出來?

我希望有幫助。

+0

不,我沒有使用WP7..I've檢查,有一個在我的解決方案沒有這樣的文件。謝謝 –

0

AssemblyName類提供Assembly FullName屬性的解析。

試試這個

string productName = (new System.Reflection.AssemblyName(this.GetType().Assembly.FullName)).Name; 
相關問題