Friday, December 28, 2012

Get Silverlight Application Last Updated Version & Date

  • Get Silverlight XAP Version
  •    
    1. Open App.xaml.cs file in your silverlight application and write following code.
      Assembly assembly = Assembly.GetExecutingAssembly(); 
      if (assembly.FullName != null) 
         string versionPart = assembly.FullName.Split(',')[1]; 
         string version = versionPart.Split('=')[1]; 
      } 

  • Get Silverlight XAP Last Updated Date.

    Below are steps to find XAP last modified date.

    1. open aspx page in which your xap file is called
    2. write following code in page_load event.

     
    
    
 
<script runat="server">
   
 public string VersionDate { get; set; }
 protected void Page_Load(object sender, EventArgs e)
 {
   string xapSource = @"ClientBin/SilverlightTest.xap";  
   string xapPath = 
       HttpContext.Current.Server.MapPath(@"") + @"\" + xapSource;
   DateTime xapCreationDate = System.IO.File.GetLastWriteTime(xapPath);
   VersionDate = xapCreationDate.ToString(new 
           System.Globalization.CultureInfo("en-US").DateTimeFormat); 
        
 }    
 </script> 
    
         3. Pass date in initParams in object tag.
 
     <object data="data:application/x-silverlight-2," 
            type="application/x-silverlight-2"
            width="100%" height="100%">
        <param name="source" value="ClientBin/SilverlightTest.xap" />
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="5.0.61118.0" />
        <param name="autoUpgrade" value="true" />
        <param name="onLoad" value="pluginLoaded" />
        <param name="initParams" 
               value="<%= string.Format("VersionDate={0}",VersionDate) %>" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" 
                    style="text-decoration: none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" 
                    alt="Get Microsoft Silverlight"
                    style="border-style: none" />
        </a>
     </object> 
 
     
         4. Go to Silverlight application and open App.xaml.cs file.

         5. you will get passed parameter in Application_Startup event.
 
    private void Application_Startup(object sender, StartupEventArgs e)
    {   
   
       if (e.InitParams.ContainsKey("VersionDate"))
       {
            System.Globalization.DateTimeFormatInfo fmt = 
                (new System.Globalization.CultureInfo("en-US")).DateTimeFormat;    
            string versionDate = DateTime.Parse(e.InitParams["VersionDate"], fmt);
       }
    }

No comments:

Post a Comment