static public bool Available(Uri updateUrl) { // Don't auto-update in the debugger if (Debugger.IsAttached) { // In debug mode we don't want to actually request the file Version = ProcessUtils.ProductVersion; Url = null; Comments = ""; return(false); } else { // TODO Display a form that shows we're doing something, otherwise it looks like the application hangs if it takes awhile to do the check string AutoUpdateIniFile = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, "AutoUpdate.ini"); using (IniFile AutoUpdateIni = new IniFile(AutoUpdateIniFile)) { // Check if there's an old installer to delete string OldUrl = AutoUpdateIni.ReadString(ProcessUtils.ProductName, "URL", ""); if (OldUrl.Length > 0) { string OldInstaller = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, Path.GetFileName(OldUrl)); FileUtils.FileDelete(OldInstaller); } // Retrieve the new INI from the updater website string NewIniFile = StringUtils.LFtoCRLF(WebUtils.HttpGet(AutoUpdateIni.ReadString("config", "URL", updateUrl.ToString()) + "?Name=" + WebUtils.UrlEncode(ProcessUtils.ProductName) + "&Version=" + WebUtils.UrlEncode(ProcessUtils.ProductVersion))); if (!string.IsNullOrEmpty(NewIniFile)) { // Save and re-open the new INI Directory.CreateDirectory(Path.GetDirectoryName(AutoUpdateIniFile)); FileUtils.FileWriteAllText(AutoUpdateIniFile, NewIniFile); using (IniFile NewIni = new IniFile(AutoUpdateIniFile)) { // Read the version, url, and comments from the latest ini Version = NewIni.ReadString(ProcessUtils.ProductName, "version", ProcessUtils.ProductVersion); Url = NewIni.ReadString(ProcessUtils.ProductName, "URL", ""); int CommentCount = NewIni.ReadInt32(ProcessUtils.ProductName, "comments", 0); if (CommentCount > 0) { Comments = NewIni.ReadString(ProcessUtils.ProductName, "comment1", ""); for (int i = 2; i <= CommentCount; i++) { Comments += Environment.NewLine + NewIni.ReadString(ProcessUtils.ProductName, "comment" + i.ToString(), ""); } } else { Comments = ""; } } } else { // The new INI was not found Version = ProcessUtils.ProductVersion; Url = null; Comments = ""; } } return(ProcessUtils.ProductVersion != Version); } }
/// <summary> /// Advanced load method that allows you to specify the section to read from /// </summary> /// <param name="sectionName">The section to read within the INI</param> /// <returns>true if the INI section existed; false otherwise</returns> protected bool Load(string sectionName) { // Store the section name SectionName = sectionName; // Load the application ini using (IniFile Ini = new IniFile(FileName, IniPassword)) { // Check if the desired section exists if (!Ini.SectionExists(sectionName)) { // Nope, so abort return(false); } // Loop through each field in the inherited class and read the value from the Ini PropertyInfo[] Properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (PropertyInfo Property in Properties) { // Ensure we only look at read+write properties (read only helper properties should not be loaded from/saved to an ini) if ((Property.CanRead) && (Property.CanWrite)) { switch (Property.PropertyType.Name) { case "Boolean": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (Boolean)Property.GetValue(this, null)), null); break; case "Boolean[]": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Byte": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (Byte)Property.GetValue(this, null)), null); break; case "Byte[]": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Char": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (Char)Property.GetValue(this, null)), null); break; case "Char[]": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "DateTime": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (DateTime)Property.GetValue(this, null)), null); break; case "DateTime[]": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Decimal": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (Decimal)Property.GetValue(this, null)), null); break; case "Decimal[]": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Double": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (Double)Property.GetValue(this, null)), null); break; case "Double[]": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Int16": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (Int16)Property.GetValue(this, null)), null); break; case "Int16[]": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Int32": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (Int32)Property.GetValue(this, null)), null); break; case "Int32[]": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Int64": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (Int64)Property.GetValue(this, null)), null); break; case "Int64[]": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "SByte": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (SByte)Property.GetValue(this, null)), null); break; case "SByte[]": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "Single": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (Single)Property.GetValue(this, null)), null); break; case "Single[]": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "String": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString()), null); break; case "String[]": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "UInt16": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (UInt16)Property.GetValue(this, null)), null); break; case "UInt16[]": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "UInt32": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (UInt32)Property.GetValue(this, null)), null); break; case "UInt32[]": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "UInt64": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (UInt64)Property.GetValue(this, null)), null); break; case "UInt64[]": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break; case "RMSecureString": string Enc = Ini.ReadString(sectionName, Property.Name, ""); if (Enc.Length > 0) { RMSecureString RMSS = new RMSecureString(); try { if (RMSecureStringPassword.Length == 0) { // No password means protected string RMSS.LoadFromProtectedString(Enc, RMSecureStringPassword); } else { // Password means encrypted string RMSS.LoadFromEncryptedString(Enc, RMSecureStringPassword); } } catch (Exception) { // Loading failed -- could be that the protection happened under a different user account, or the password is incorrect // TODO Should really save the exception and throw it later I think RMSS = new RMSecureString(); } Property.SetValue(this, RMSS, null); } break; case "StringDictionary": StringDictionary SD = new StringDictionary(); string[] Keys = Ini.ReadSection(sectionName); foreach (string Key in Keys) { if (Key.IndexOf(Property.Name + "_", StringComparison.OrdinalIgnoreCase) == 0) { string KeyWithoutPrefix = Key.Substring(Property.Name.Length + 1); SD.Add(KeyWithoutPrefix, Ini.ReadString(sectionName, Key, "")); } } Property.SetValue(this, SD, null); //string Section = Property.Name.ToUpper(); //string[] Keys = Ini.ReadSection(Section); //StringDictionary SD = new StringDictionary(); //foreach (string Key in Keys) //{ // SD.Add(Key, Ini.ReadString(Section, Key, "")); //} //Property.SetValue(this, SD, null); break; default: // Check for enum, which we can try to parse if (Property.PropertyType.BaseType.Name == "Array") { List <int> EnumValues = new List <int>(); string[] StringValues = Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null)); foreach (string StringValue in StringValues) { EnumValues.Add((int)Enum.Parse(Property.PropertyType.GetElementType(), StringValue)); } Property.SetValue(this, EnumValues.ToArray(), null); } else if (Property.PropertyType.BaseType.Name == "Enum") { Property.SetValue(this, Enum.Parse(Property.PropertyType, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString())), null); } break; } } } } Loaded = true; return(true); }