using Microsoft.Win32; using System; using System.Globalization; using System.IO; using System.Xml; namespace Microsoft.Samples.AppUpdater { public class AppStartConfig { public enum LaunchModes { AppDomain, Process } private string ConfigPath; private string _AppFolderName; private string _AppExeName; private string _SetupVersion; private LaunchModes _AppLaunchMode; public string AppFolderName { get { return _AppFolderName; } set { _AppFolderName = value; } } public string AppExeName { get { return _AppExeName; } set { _AppExeName = value; } } public string SetupVersion { get { return _SetupVersion; } set { _SetupVersion = value; } } public LaunchModes AppLaunchMode { get { return _AppLaunchMode; } set { _AppLaunchMode = value; } } public string AppExePath { get { return Path.Combine(Path.GetDirectoryName(ConfigPath), AppFolderName) + "\\" + AppExeName; } set { } } public string AppPath { get { return Path.Combine(Path.GetDirectoryName(ConfigPath), AppFolderName) + "\\"; } set { } } public AppStartConfig(string filePath) { ConfigPath = filePath; } public static AppStartConfig Load(string filePath) { AppStartConfig appStartConfig = new AppStartConfig(filePath); try { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(filePath); } catch (Exception innerException) { throw new ConfigFileMissingException("Config file '" + filePath + "' is missing.", innerException); } string text = ""; try { XmlNode xmlNode = xmlDocument.SelectSingleNode("//AppRedirectionKey"); text = xmlNode.InnerText; } catch (Exception) { text = ""; } if (text != "") { return LoadRemoteConfigFile(text, filePath); } XmlNode xmlNode2 = xmlDocument.SelectSingleNode("//AppFolderName"); appStartConfig.AppFolderName = xmlNode2.InnerText; XmlNode xmlNode3 = xmlDocument.SelectSingleNode("//AppExeName"); appStartConfig.AppExeName = xmlNode3.InnerText; XmlNode xmlNode4 = xmlDocument.SelectSingleNode("//AppLaunchMode"); if (xmlNode4 == null) { appStartConfig.AppLaunchMode = LaunchModes.Process; } else if (xmlNode4.InnerText.ToLower(new CultureInfo("en-US")) == "appdomain") { appStartConfig.AppLaunchMode = LaunchModes.AppDomain; } else { appStartConfig.AppLaunchMode = LaunchModes.Process; } XmlNode xmlNode5 = xmlDocument.SelectSingleNode("//SetupVersion"); appStartConfig.SetupVersion = xmlNode5.InnerText; return appStartConfig; } catch (Exception ex2) { throw ex2; } } public void Udpate() { try { XmlDocument xmlDocument = new XmlDocument(); XmlElement xmlElement = xmlDocument.CreateElement("Config"); xmlDocument.AppendChild(xmlElement); XmlElement xmlElement2 = xmlDocument.CreateElement("AppFolderName"); xmlElement2.InnerText = AppFolderName; xmlElement.AppendChild(xmlElement2); xmlElement2 = xmlDocument.CreateElement("AppExeName"); xmlElement2.InnerText = AppExeName; xmlElement.AppendChild(xmlElement2); xmlElement2 = xmlDocument.CreateElement("SetupVersion"); xmlElement2.InnerText = SetupVersion; xmlElement.AppendChild(xmlElement2); xmlElement2 = xmlDocument.CreateElement("AppLaunchMode"); if (AppLaunchMode == LaunchModes.AppDomain) { xmlElement2.InnerText = "appdomain"; } else { xmlElement2.InnerText = "process"; } xmlElement.AppendChild(xmlElement2); xmlDocument.Save(ConfigPath); } catch (Exception ex) { throw ex; } } private static AppStartConfig LoadRemoteConfigFile(string key, string orgConfigPath) { string text = ""; try { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key); text = (string)registryKey.GetValue("InstallDir"); text = Path.Combine(text, Path.GetFileName(orgConfigPath)); return Load(text); } catch (Exception ex) { throw ex; } } } }