2011-03-02 2 views
2

Silverlight 응용 프로그램의 ProductName을 프로그래밍 방식으로 가져올 수 있습니까?Silverlight에서 응용 프로그램의 ProductName을 얻는 방법

string productName = System.Windows.Forms.Application.ProductName; 

당신에게, 속성 폴더에서 WMAppManifest라는 파일을 포함 어떤 장치에 대한 deving을하지만, WP7의 실버 라이트에 대한 확인

답변

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; 
} 

(입력 어셈블리에 메서드가있는 경우 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; 
} 
+0

죄송합니다. 두 번째 코드 샘플에서 'FindIndexOf'라는 확장 메서드 중 하나를 사용했음을 알게되었습니다. 요청에 따라 코드를 공유하게되어 기쁩니다. – base2

0

하지 감사 :이 윈폼/WPF 명령의 실버 상당을 찾고 있어요 .xml이고 배포> 응용 프로그램에서 Title이라는 필드가 있으면 XML을 끌어낼 수 있습니까?

도움이 되었기를 바랍니다.

+0

을보십시오. 감사합니다. –

0

AssemblyName 클래스는 Assembly FullName 속성의 구문 분석을 제공합니다.

아니 내가 WP7..I've 체크하고 내 솔루션에서 그런 파일이 없습니다 사용하지 않을거야이

string productName = (new System.Reflection.AssemblyName(this.GetType().Assembly.FullName)).Name; 
관련 문제