本文整理汇总了C#中System.Configuration.ConfigurationErrorsException类的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationErrorsException类的具体用法?C# ConfigurationErrorsException怎么用?C# ConfigurationErrorsException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConfigurationErrorsException类属于System.Configuration命名空间,在下文中一共展示了ConfigurationErrorsException类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: CustomSection
//引入命名空间
using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
namespace Samples.AspNet
{
// Define a custom section.
public sealed class CustomSection :
ConfigurationSection
{
public CustomSection()
{
}
[ConfigurationProperty("fileName", DefaultValue = "default.txt",
IsRequired = true, IsKey = false)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string FileName
{
get
{
return (string)this["fileName"];
}
set
{
this["fileName"] = value;
}
}
[ConfigurationProperty("maxUsers", DefaultValue = (long)10,
IsRequired = false)]
[LongValidator(MinValue = 1, MaxValue = 100,
ExcludeRange = false)]
public long MaxUsers
{
get
{
return (long)this["maxUsers"];
}
set
{
this["maxUsers"] = value;
}
}
}
// Create the custom section and write it to
// the configuration file.
class UsingConfigurationErrorsException
{
// 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);
}
}
// 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);
}
}
static void Main(string[] args)
{
ModifyCustomSection();
}
}
}
开发者ID:.NET开发者,项目名称:System.Configuration,代码行数:131,代码来源:ConfigurationErrorsException
注:本文中的System.Configuration.ConfigurationErrorsException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论