How can I programmatically extract the app name and version number?

I want to populate a WAMS table in the UnhandledException event, and I've got this code:

private async void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs args)
{
    if (Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        Debugger.Break();
    }
    PLATYPIRUS_WAMS_EXCEPTIONLOG pruwamsel = new PLATYPIRUS_WAMS_EXCEPTIONLOG();
    pruwamsel.appNameAndVersion = "Platypi R Us for WP8 v. 3.14";
    pruwamsel.ExceptionMsg = args.ExceptionObject.Message;
    pruwamsel.InnerException = args.ExceptionObject.InnerException.ToString();
    pruwamsel.ExceptionToStr = args.ToString();
    pruwamsel.dateTimeOffsetStamp = DateTimeOffset.UtcNow;
    await App.MobileService.GetTable<PLATYPIRUS_WAMS_EXCEPTIONLOG>().InsertAsync(pruwamsel); 
}

...but I don't really want to hardcode the app name and version. How can I extract those programmatically?

UPDATE

Incorporating the two ideas, I end up with:

private async void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs args)
{
    if (Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        Debugger.Break();
    }

    string appName;
    string appVersion;
    var xmlReaderSettings = new XmlReaderSettings
    {
        XmlResolver = new XmlXapResolver()
    };

    using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
    {
        xmlReader.ReadToDescendant("App");

        appName = xmlReader.GetAttribute("Title");
        appVersion = xmlReader.GetAttribute("Version");
    }

    PLATYPIRUS_WAMS_EXCEPTIONLOG pruwamsel = new PLATYPIRUS_WAMS_EXCEPTIONLOG();
    pruwamsel.appNameAndVersion = string.Format("{0} {1}", appName, appVersion);
    pruwamsel.ExceptionMsg = args.ExceptionObject.Message;
    pruwamsel.InnerException = args.ExceptionObject.InnerException.ToString();
    pruwamsel.ExceptionToStr = args.ExceptionObject.ToString();
    pruwamsel.dateTimeOffsetStamp = DateTimeOffset.UtcNow; 
    await App.MobileService.GetTable<PLATYPIRUS_WAMS_EXCEPTIONLOG>().InsertAsync(pruwamsel); 
}

Upvotes: 0

Views: 687

Answers (1)

Pedro Lamas
Pedro Lamas

Reputation: 7243

The application name and version are registered in WMAppManifest.xml file.

By using this this sample and replacing the parts refering to "ProductID" with "Title" and "Version", I managed to get to this code:

var xmlReaderSettings = new XmlReaderSettings
{
    XmlResolver = new XmlXapResolver()
};

using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
{
    xmlReader.ReadToDescendant("App");

    var AppName = xmlReader.GetAttribute("Title");
    var AppVersion = xmlReader.GetAttribute("Version");
}

Upvotes: 3

Related Questions