In Windows Phone 7, is there a way to get the application build or compile date in code? I would like to display the date, along with the version number, for support purposes for my 开发者_StackOverflow中文版application.
If it isn't immediately available, any hints or alternatives? (I guess one is making it an app setting, which is hokey).
You can parse the version number out of Assembly.GetExecutingAssembly().FullName
.
The output is of this form
PhoneApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
The build date doesn't appear to be available, and arguably you won't need it if you increment your version number every release. Alteranatively you could store this somewhere else if it's important to your app.
The date (& time) of the build isn't included in an assembly.
If you could get to the file system on the phone you may be able to get a date from this but it may be affected by the marketplace ingestion process (when the code is signed) and so you may not be able to guarantee this.
If you used a *
for the build part of the version number and then work out the build date from that. (It's the number of days since 2000-01-01.)
Or, you could add something to your build process to set a property or setting.
Or, if using SVN for your version control system, you could use $WCDATE$
in a template with SubWcRev.exe to set this.
Or, you could add this to the app through the use of T4.
The following in a TT file should do the trick:
<#@ template language="C#" #>
<#@ import namespace="System" #>
using System.Windows;
namespace MyNamespace
{
public partial class App : Application
{
public string BuildDate { get { return "<#= DateTime.Now #>"; } }
}
}
To get the App Version on Windows Phone 7+: https://stackoverflow.com/a/22838743/1033581
Here is the WP7 code:
var xmlReaderSettings = new XmlReaderSettings
{
XmlResolver = new XmlXapResolver()
};
using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
{
xmlReader.ReadToDescendant("App");
return xmlReader.GetAttribute("Version");
}
To get the App Version on Windows Phone 8+: https://stackoverflow.com/a/23387825/1033581
Here is the WP8 code:
using (var stream = new FileStream("WMAppManifest.xml", FileMode.Open, FileAccess.Read))
{
var appVersion = XElement.Load(stream).Descendants("App").FirstOrDefault().Attribute("Version");
return appVersion != null ? appVersion.Value : null;
}
- add file BuildDate.txt
- project Properties > Build Events
- Pre-build event command line: echo %date% %time% > "$(ProjectDir)\BuildDate.txt"
Add code:
private static DateTime UpdatedAt() { var streamResourceInfo = Application.GetResourceStream(new Uri("BuildDate.txt", UriKind.Relative)); var reader = new StreamReader(streamResourceInfo.Stream); string text = reader.ReadToEnd(); var substring = text.Substring(0, text.Length - 6); // text = "11.05.2014 20:44:52,07 \n\r" var exact = DateTime.ParseExact(substring, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture); return exact; }
精彩评论