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