作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以通过编程方式获取 Silverlight 应用程序的 ProductName?我正在寻找与此 WinForms/WPF 指令等效的 Silverlight:
string productName = System.Windows.Forms.Application.ProductName;
谢谢
最佳答案
您可以使用以下方法,但前提是您可以保证从“entry”程序集中调用它。
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;
}
(如果该方法位于您的入口程序集中,您可以将 GetCallingAssembly 替换为 GetExecutingAssembly)。
我还弄清楚了如何从 .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;
}
关于silverlight - 如何在 Silverlight 中获取应用程序的 ProductName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169461/
我是一名优秀的程序员,十分优秀!