// Change custom section and write it to disk. static void SerializeCustomSection() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection customSection = config.Sections.Get("CustomSection") as CustomSection; TimeSpan ts = new TimeSpan(1, 30, 30); customSection.MaxIdleTime = ts; config.Save(); string maxIdleTime = customSection.MaxIdleTime.ToString(); Console.WriteLine("New max idle time: {0}", maxIdleTime); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void SetTimeDelay() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection section = config.Sections.Get("CustomSection") as CustomSection; TimeSpan td = new TimeSpan(); td = TimeSpan.FromMinutes( DateTime.Now.Minute); section.TimeDelay = td; section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); config.Save(); Console.WriteLine("timeDelay: {0}", section.TimeDelay.ToString()); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
// Create a custom section. static void CreateSection() { try { CustomSection customSection; // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Create the section entry // in the <configSections> and the // related target section in <configuration>. if (config.Sections["CustomSection"] == null) { customSection = new CustomSection(); config.Sections.Add("CustomSection", customSection); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
//</Snippet2> //<Snippet5> static void ChangeDefaults() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection customSection = config.Sections["CustomSection"] as CustomSection; // This will fail if the ReadOnly flag is // set to true in GetRuntimeObject. customSection.FileName = "newFile.txt"; TimeSpan ts = new TimeSpan(0, 15, 0); customSection.MaxIdleTime += ts; customSection.MaxUsers += 100; if (!customSection.ElementInformation.IsLocked) { config.Save(); ConfigurationManager.RefreshSection("CustomSection"); } else { Console.WriteLine("Section was locked, could not update."); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
public static void SetPermission() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection section = config.Sections.Get("CustomSection") as CustomSection; section.Permission = CustomSection.Permissions.FullControl; section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); config.Save(); Console.WriteLine("Current Protection: {0}", section.Permission.ToString()); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
// Create a custom section and save it in the // application configuration file. public void CreateCustomSection() { Configuration config = WebConfigurationManager.OpenWebConfiguration("/ConfigSite"); CustomSection section = config.Sections["CustomSection"] as CustomSection; if (section == null) { // Create section and add it to the configuration. section = new CustomSection(); config.Sections.Add("CustomSection", section); } // Assign configuration settings. section.aTimeout = TimeSpan.FromSeconds(DateTime.Now.Second); section.anInteger = 1500; section.aString = "Hello World"; // Save the changes. config.Save(); }
// Modify a custom section and cause configuration // error exceptions. static void ModifyCustomSection() { try { // Get the application configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection custSection = config.Sections["CustomSection"] as CustomSection; // Change the section properties. custSection.FileName = "newName.txt"; // Cause an exception. custSection.MaxUsers = custSection.MaxUsers + 100; if (!custSection.ElementInformation.IsLocked) { config.Save(); } else { Console.WriteLine( "Section was locked, could not update."); } } catch (ConfigurationErrorsException err) { string msg = err.Message; Console.WriteLine("Message: {0}", msg); string fileName = err.Filename; Console.WriteLine("Filename: {0}", fileName); int lineNumber = err.Line; Console.WriteLine("Line: {0}", lineNumber.ToString()); string bmsg = err.BareMessage; Console.WriteLine("BareMessage: {0}", bmsg); string source = err.Source; Console.WriteLine("Source: {0}", source); string st = err.StackTrace; Console.WriteLine("StackTrace: {0}", st); } }
public static void GetTypeName() { try { CustomSection section = ConfigurationManager.GetSection("CustomSection") as CustomSection; Console.WriteLine("CustomSection type: {0}", section); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
public static void GetTimeDelay() { try { CustomSection section = ConfigurationManager.GetSection("CustomSection") as CustomSection; Console.WriteLine("timeDelay: {0}", section.TimeDelay.ToString()); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
public static void GetPermission() { try { CustomSection section = ConfigurationManager.GetSection("CustomSection") as CustomSection; Console.WriteLine("Default Permission: {0}", section.Permission.ToString()); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
// Create a custom section. static UsingConfigurationErrorsException() { // Get the application configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // If the section does not exist in the configuration // file, create it and save it to the file. if (config.Sections["CustomSection"] == null) { CustomSection custSection = new CustomSection(); config.Sections.Add("CustomSection", custSection); custSection = config.GetSection("CustomSection") as CustomSection; custSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } }
// Get the custom section stored in // the configuration file. public string GetCustomSection() { Configuration config = WebConfigurationManager.OpenWebConfiguration("/ConfigSite"); CustomSection section = config.Sections["CustomSection"] as CustomSection; string currentSection = string.Empty; if (section != null) { currentSection = HttpContext.Current.Server.HtmlEncode( section.SectionInformation.GetRawXml()); } else { currentSection = "CustomSection does not exist"; } return(currentSection); }
// Read custom section from disk. static void DeserializeCustomSection() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection customSection = config.Sections.Get("CustomSection") as CustomSection; TimeSpan maxIdleTime = customSection.MaxIdleTime; Console.WriteLine("Max idle time: {0}", maxIdleTime.ToString()); } catch (Exception e) { Console.WriteLine(e.ToString()); } }