/// <summary> /// Attempts to increment the RunCount option for the specified version of the specified Type /// </summary> /// <param name="configuration">The configuration in which the option will be found</param> /// <param name="type">The Type for which the RunCount value will be incremented</param> /// <returns></returns> public static bool IncrementRunCountForVersionOfType(XmlConfiguration configuration, Type type, out DateTime installDate, out int runCount) { installDate = DateTime.Now; runCount = 0; try { XmlConfigurationCategory category = InstallationEngine.GetExistingCategoryForTypeVersion(configuration, type, CategoryNames.Installed); if (category != null) { XmlConfigurationOption option = null; option = category.Options[@"InstallDate"]; if (option != null) { installDate = (DateTime)option.Value; } option = category.Options[@"RunCount"]; if (option != null) { // retrieve, increment, and set the run count runCount = (int)option.Value; runCount++; option.Value = runCount; } } } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } return(false); }
/// <summary> /// Reads properties for the window from the configuration. Properties include Size, Location, and WindowState. /// </summary> /// <param name="window">The window whose properties were previously persisted</param> /// <param name="configuration">The configuration to read from</param> public static void Read(Form window, XmlConfiguration configuration) { if (window != null) { if (configuration != null) { string categoryName = @"Persisted Objects\Windows\" + window.GetType().FullName; XmlConfigurationCategory category = null; // load the category if ((category = configuration.Categories[categoryName]) != null) { XmlConfigurationOption option = null; // load the location if ((option = category.Options["Location"]) != null) { window.Location = (Point)option.Value; } if ((option = category.Options["Size"]) != null) { window.Size = (Size)option.Value; } if ((option = category.Options["WindowState"]) != null) { window.WindowState = (FormWindowState)option.Value; } } } } }
/// <summary> /// Writes the data for the window we are listening to, to the appropriate category in the specified configuration /// </summary> private void WriteWindowData() { try { XmlConfigurationEventArgs e = new XmlConfigurationEventArgs(null, XmlConfigurationElementActions.None); this.OnNeedsConfiguration(this, e); if (e.Element == null) { return; } // retrive the configuration where this listener will read and write XmlConfiguration configuration = e.Element; if (configuration != null) { XmlConfigurationCategory category = configuration.Categories[_path, true]; if (category != null) { XmlConfigurationOption option = null; if ((option = category.Options["Size"]) == null) { option = category.Options["Size", true, _size]; option.Description = "The size of the window (Width & Height)"; option.Category = "Layout"; option.ShouldSerializeValue = true; } option.Value = _size; if ((option = category.Options["Location"]) == null) { option = category.Options["Location", true, _location]; option.Description = "The location of the top left corner of the window (Left & Top)"; option.Category = "Layout"; option.ShouldSerializeValue = true; } option.Value = _location; if ((option = category.Options["WindowState"]) == null) { option = category.Options["WindowState", true, _state]; option.Description = "The state of the window (Normal, Maximized, or Minimized)"; option.Category = "Layout"; // option.ShouldSerializeValue = true; } option.Value = _state; } } } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } }
public static bool RemoveUninstalledEntryForType(XmlConfiguration configuration, Type type) { XmlConfigurationCategory category = null; // find and remove the install category for this type category = InstallationEngine.GetExistingCategoryForTypeVersion(configuration, type, CategoryNames.Uninstalled); if (category != null) { category.Remove(); category = null; } return(true); }
/// <summary> /// Writes properties of the window to the configuration. Properties include Size, Location, and WindowState. /// </summary> /// <param name="window">The window whose properties will be written with the following pameters</param> /// <param name="size">The size of the window</param> /// <param name="location">The location of the window</param> /// <param name="windowState">The state of the window</param> /// <param name="configuration">The configuration to write to</param> public static void Write(Form window, Size size, Point location, FormWindowState windowState, XmlConfiguration configuration) { if (window != null) { if (configuration != null) { string categoryName = @"Persisted Objects\Windows\" + window.GetType().FullName; XmlConfigurationCategory category = null; // load the category if ((category = configuration.Categories[categoryName, false]) == null) { category = configuration.Categories[categoryName, true]; // category.Hidden = true; } XmlConfigurationOption option = null; // load the location if ((option = category.Options["Location", false]) == null) { option = category.Options["Location", true, window.Location]; option.Category = "Layout"; option.Description = "The coordinates of the upper-left corner of the window relative to it's container."; option.ShouldSerializeValue = true; } option.Value = location; if ((option = category.Options["Size", false]) == null) { option = category.Options["Size", true, window.Size]; option.Category = "Layout"; option.Description = "The size of the window."; option.ShouldSerializeValue = true; } option.Value = size; if ((option = category.Options["WindowState", false]) == null) { option = category.Options["WindowState", true, window.WindowState]; option.Category = "Layout"; option.Description = "The state of the window (ie. Maximized, Minimized, Normal)."; // option.ShouldSerializeValue = true; } option.Value = windowState; } } }
/// <summary> /// Reads the window data from the configuration /// </summary> private void ReadWindowData() { try { XmlConfigurationEventArgs e = new XmlConfigurationEventArgs(null, XmlConfigurationElementActions.None); this.OnNeedsConfiguration(this, e); if (e.Element == null) { return; } // retrive the configuration where this listener will read and write XmlConfiguration configuration = e.Element; if (configuration != null) { XmlConfigurationCategory category = configuration.Categories[_path, false]; if (category != null) { XmlConfigurationOption option = null; if ((option = category.Options["Size"]) != null) { _size = (Size)option.Value; } if ((option = category.Options["Location"]) != null) { _location = (Point)option.Value; } if ((option = category.Options["WindowState"]) != null) { _state = (FormWindowState)option.Value; } } } } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } }
/// <summary> /// Creates a category for the type including it's version (defaulted to "1.0.0.0" for versionless types) in the specified configuration, using the specified type, and specified category name. /// </summary> /// <param name="configuration">The configuration in which the category will be created</param> /// <param name="type">The type for which the category will be created</param> /// <param name="categoryName">The category name in which creation will occur</param> /// <returns></returns> public static XmlConfigurationCategory CreateCategoryForTypeVersion(XmlConfiguration configuration, Type type, CategoryNames categoryName) { try { if (configuration != null) { if (type != null) { // start in the specified category XmlConfigurationCategory searchCategory = configuration.Categories[@"SnapIns\" + categoryName.ToString()]; if (searchCategory != null) { SnapInAttributeReader reader = new SnapInAttributeReader(type); if (reader != null) { Version version = null; SnapInVersionAttribute versionAttribute = reader.GetSnapInVersionAttribute(); if (versionAttribute != null) { version = versionAttribute.Version; } else { version = new Version(1, 0, 0, 0); } string path = string.Format("{0}\\{1}\\{2}", @"SnapIns\" + categoryName.ToString(), type.FullName, version.ToString()); return(configuration.Categories[path, true]); } } } } } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } return(null); }
/// <summary> /// Parses the category specified in the configuration specified for the type specified, and returns an array of Versions that are listed in the category. /// </summary> /// <param name="configuration">The configuration to parse</param> /// <param name="type">The type to search for</param> /// <param name="categoryName">The category to search in</param> /// <returns></returns> public static Version[] GetListedVersionsOfType(XmlConfiguration configuration, Type type, CategoryNames categoryName) { try { if (configuration != null) { if (type != null) { // start in the specified category XmlConfigurationCategory searchCategory = configuration.Categories[@"SnapIns\" + categoryName.ToString()]; if (searchCategory != null) { // enumerate the type category, each subcategory is the full name of a type that is listed foreach (XmlConfigurationCategory typeCategory in searchCategory.Categories) { // compare the category's element name to the full name of the type specified if (string.Compare(typeCategory.ElementName, type.FullName, false) == 0) { // enumerate the version category, each subcategory is the version number of the type ArrayList array = new ArrayList(); foreach (XmlConfigurationCategory versionCategory in typeCategory.Categories) { Version v = new Version(versionCategory.ElementName); array.Add(v); } return(array.ToArray(typeof(Version)) as Version[]); } } } } } } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } return(new Version[] {}); }
/// <summary> /// Creates an installation record for the specified Type /// </summary> /// <param name="configuration">The configuration in which the category will be created</param> /// <param name="type">The type for which the category will be created</param> /// <returns></returns> public static XmlConfigurationCategory InstallVersionOfType(XmlConfiguration configuration, Type type, out DateTime installDate) { installDate = DateTime.Now; // remove the uninstall entry for the type InstallationEngine.RemoveUninstalledEntryForType(configuration, type); // create the install entry for the type XmlConfigurationCategory category = InstallationEngine.CreateCategoryForTypeVersion(configuration, type, CategoryNames.Installed); if (category != null) { XmlConfigurationOption option = null; // create the install date option = category.Options[@"InstallDate"]; if (option == null) { option = category.Options[@"InstallDate", true, installDate]; option.Category = @"Installation Notes"; option.Description = @"This date marks the date and time on which the SnapIn was installed."; option.ShouldSerializeValue = true; } option = null; // create the run count option = category.Options[@"RunCount"]; if (option == null) { option = category.Options[@"RunCount", true, 0]; option.Category = @"Installation Notes"; option.Description = @"This number indicates the number of times the SnapIn has executed."; } option = null; } return(category); }
/// <summary> /// Creates an uninstallation record for the specified Type /// </summary> /// <param name="configuration">The configuration in which the category will be created</param> /// <param name="type">The type for which the category will be created</param> /// <returns></returns> public static XmlConfigurationCategory UninstallVersionOfType(XmlConfiguration configuration, Type type) { // remove the install entry for the type InstallationEngine.RemoveInstalledEntryForType(configuration, type); // create the uninstall entry for the type XmlConfigurationCategory category = InstallationEngine.CreateCategoryForTypeVersion(configuration, type, CategoryNames.Uninstalled); if (category != null) { XmlConfigurationOption option = null; // create the install date option = category.Options[@"UninstallDate"]; if (option == null) { option = category.Options[@"UninstallDate", true, DateTime.Now.ToString()]; option.Category = @"Installation Notes"; option.Description = @"This date marks the date and time on which the SnapIn was uninstalled."; } option = null; } return(category); }
/// <summary> /// Saves the AutoUpdate options specified. /// </summary> /// <param name="options">The options to retrieve the value of the option to save.</param> /// <param name="optionToSave">The name of the option to save.</param> public static void Save(AutoUpdateOptions options, AutoUpdateOptions defaultOptions, AutoUpdateOptionNames optionName) { if (options == null) { throw new ArgumentNullException("options"); } if (defaultOptions == null) { throw new ArgumentNullException("defaultOptions"); } try { XmlConfiguration configuration = AllUsersConfigurationProvider.Current.Configuration; XmlConfigurationCategory category = configuration.Categories[ConfigurationCategoryName, true]; XmlConfigurationOption option = null; // this could be refactored again // into another method, but i'm in a massive hurry today switch (optionName) { case AutoUpdateOptionNames.AutomaticallyCheckForUpdates: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AutomaticallyCheckForUpdates; break; case AutoUpdateOptionNames.AutomaticallyDownloadUpdates: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AutomaticallyDownloadUpdates; break; case AutoUpdateOptionNames.AutomaticallyInstallUpdates: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AutomaticallyInstallUpdates; break; case AutoUpdateOptionNames.AutomaticallySwitchToNewVersion: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AutomaticallySwitchToNewVersion; break; case AutoUpdateOptionNames.AutomaticallyUpdateAlternatePath: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AutomaticallyUpdateAlternatePath; break; case AutoUpdateOptionNames.AlternatePath: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.AlternatePath; break; case AutoUpdateOptionNames.WebServiceUrl: if ((option = category.Options[optionName.ToString()]) == null) { Load(defaultOptions, optionName); option = category.Options[optionName.ToString()]; } option.Value = options.WebServiceUrl; break; } ; } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// Loads the value of the option specified. Creates the option if it does not exist, and writes the default value to the option. /// </summary> /// <param name="defaultOptions">The default options to use to create the option if the option does not exist.</param> /// <param name="optionName">The name of the option to load.</param> /// <returns></returns> public static object Load(AutoUpdateOptions defaultOptions, AutoUpdateOptionNames optionName) { if (defaultOptions == null) { throw new ArgumentNullException("defaultOptions"); } try { XmlConfiguration configuration = AllUsersConfigurationProvider.Current.Configuration; XmlConfigurationCategory category = configuration.Categories[ConfigurationCategoryName, true]; XmlConfigurationOption option = null; switch (optionName) { case AutoUpdateOptionNames.AutomaticallyCheckForUpdates: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyCheckForUpdates]; option.DisplayName = @"Automatically Check for Updates"; option.Category = @"General"; option.Description = @"Determines whether the AutoUpdateManager automatically checks for updates on startup."; } break; case AutoUpdateOptionNames.AutomaticallyDownloadUpdates: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyDownloadUpdates]; option.DisplayName = @"Automatically Download Updates"; option.Category = @"General"; option.Description = @"Determines whether the AutoUpdateManager automatically downloads available updates without prompting."; } break; case AutoUpdateOptionNames.AutomaticallyInstallUpdates: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyInstallUpdates]; option.DisplayName = @"Automatically Install Updates"; option.Category = @"General"; option.Description = @"Determines whether the AutoUpdateManager automatically installs the updates after downloading."; } break; case AutoUpdateOptionNames.AutomaticallySwitchToNewVersion: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallySwitchToNewVersion]; option.DisplayName = @"Automatically Switch to Newest Version"; option.Category = @"General"; option.Description = @"Determines whether the AutoUpdateManager automatically switches to the newest version after installation."; } break; case AutoUpdateOptionNames.AutomaticallyUpdateAlternatePath: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyUpdateAlternatePath]; option.DisplayName = @"Automatically Update Alternate Path"; option.Category = @"General"; option.Description = @"Determines whether the AutoUpdateManager automatically creates backup copies of the .Manifest and .Update files after installation."; } break; case AutoUpdateOptionNames.AlternatePath: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.AlternatePath]; option.DisplayName = @"Alternate Download Path"; option.Category = @"General"; option.Description = @"This alternate path (url or unc path) will be checked first before attempting to use the web service url to locate updates."; option.EditorAssemblyQualifiedName = typeof(FolderBrowserUITypeEditor).AssemblyQualifiedName; } break; case AutoUpdateOptionNames.WebServiceUrl: if ((option = category.Options[optionName.ToString()]) == null) { option = category.Options[optionName.ToString(), true, defaultOptions.WebServiceUrl]; option.DisplayName = @"Web Service Url"; option.Category = @"General"; option.Description = @"The url specifying where the AutoUpdate Web Service can be located."; } break; } ; return(option.Value); } catch (Exception ex) { Debug.WriteLine(ex); } return(null); }