Tag: 配置

App.config中的自定义配置部分C#

我是c#中配置部分的初学者 我想在配置文件中创建自定义部分。 我在google搜索后尝试的内容如下 配置文件: CustomSection.cs namespace CustomSectionTest { public class CustomSection : ConfigurationSection { [ConfigurationProperty(“key”, DefaultValue=”Default”, IsRequired = true)] [StringValidator(InvalidCharacters = “~!@#$%^&*()[]{}/;’\”|\\”, MinLength = 1, MaxLength = 60)] public String Key { get { return this[“key”].ToString(); } set { this[“key”] = value; } } } } 当我使用此代码检索Section时,我收到一条错误说配置错误。 var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection(“CustomSection”); 我错过了什么? 谢谢。 编辑 我最终需要的是

如何在app.config中以编程方式修改assemblyBinding?

我试图通过使用XmlDocument类并直接修改值来在安装时更改bindingRedirect元素。 这是我的app.config看起来像: … … 然后我尝试使用以下代码将1.0更改为2.0 private void SetRuntimeBinding(string path, string value) { XmlDocument xml = new XmlDocument(); xml.Load(Path.Combine(path, “MyApp.exe.config”)); XmlNode root = xml.DocumentElement; if (root == null) { return; } XmlNode node = root.SelectSingleNode(“/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion”); if (node == null) { throw (new Exception(“not found”)); } node.Value = value; xml.Save(Path.Combine(path, “MyApp.exe.config”)); } 但是,它会引发“未找到”的exception。 如果我将路径备份到/ configuration / runtime它就可以了。 […]

Windows服务无法从我的Installer构造函数中访问app.config

我想将我的Windows服务’登录的用户名/密码信息存储为app.config中的’用户。 所以在我的安装程序中,我试图从app.config中获取用户名/密码并设置属性,但是在尝试安装服务时遇到错误。 如果我硬编码用户名/密码,它工作正常,当我尝试访问app.config时失败 public class Blah : Installer { public Blah() { ServiceProcessInstaller oServiceProcessInstaller = new ServiceProcessInstaller(); ServiceInstaller oServiceInstaller = new ServiceInstaller(); oServiceProcessInstaller.Account = ServiceAccount.User; oServiceProcessInstaller.Username = ConfigurationManager.AppSettings[“ServiceProcessUsername”].ToString(); } }

如何在运行时更新配置文件中的assemblyBinding部分?

我正在尝试动态更改程序集绑定(从一个版本到另一个版本)。 我试过这段代码,但它不起作用: Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection assemblyBindingSection = config.Sections[“assemblyBinding”]; assemblyBindingSection.SectionInformation.ConfigSource = “bindingConf.xml”; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(“assemblyBinding”); with bindingConf.xml包含assemblyBinding部分配置。 那么可以在运行时更改此部分吗? 怎么做? 我有什么替代品?

在Enterprise Library中更改运行时的连接字符串

有没有办法在运行时更改Enterprise Library中的DataBase对象的连接字符串? 我找到了这个链接,但它有点过时(2005) 我也发现了这个,但它似乎一般适用于.Net,我想知道是否有一些东西可以专门为EntLib完成。 我只是将连接字符串名称传递给DatabaseFactory对象中的CreateDatabase()方法,直到昨天我的项目经理要求我支持多个数据库实例。 碰巧我们必须为每个状态设置一个数据库(一个用于CA,一个用于FL等),因此我的软件需要遍历所有数据库并对数据执行某些操作,但它将使用相同的配置文件。 提前致谢。

使用c#中的filter的log4net纯代码配置

我试图完全按代码配置Log4Net,但是当我使用最小配置时,我被NHibernate和流畅的界面记录消息所淹没。 所以,我想做的很简单。 告诉Log4Net只显示我的单个类的日志消息。 我玩弄了一下,但无法搞清楚…… 任何人都可以提供帮助,我认为以下代码说明了我的想法: var filter = new log4net.Filter.LoggerMatchFilter(); filter.LoggerToMatch = typeof(DatabaseDirectory).ToString(); filter.AcceptOnMatch = false; var x = new log4net.Appender.ConsoleAppender(); x.Layout = new log4net.Layout.SimpleLayout(); x.AddFilter(filter); log4net.Config.BasicConfigurator.Configure(x); 好的,谢谢你的帮助,但这里肯定有一些问题。 但我越来越近了。 我尝试了XML配置,它有更多的文档。 我设法使用以下XML配置实现了所需的结果。 上面的“纯代码”版本中必定存在一些错误配置。 以下XML配置提供“正确”输出,这与上面代码中的配置不同。 有谁看到了差异?

CS0122:由于其保护级别,’System.Configuration.StringUtil’无法访问

我目前正在做一个学校项目,我可以让学生和老师搜索相关的工作。 我试图进入ASP.Net配置添加用户和角色,但我收到此错误。 – ‘/asp.netwebadminfiles’应用程序中的服务器错误。 编译错误说明:在编译为此请求提供服务所需的资源期间发生错误。 请查看以下特定错误详细信息并相应地修改源代码。 编译器错误消息:CS0122:由于其保护级别,’System.Configuration.StringUtil’无法访问 来源错误: Line 987: Line 988: // Put together some unique app id Line 989: string appId = StringUtil.GetNonRandomizedHashCode(String.Concat(appPath, appPhysPath)).ToString(“x”, CultureInfo.InvariantCulture); Line 990: Line 991: 源文件:c:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ ASP.NETWebAdminFiles \ App_Code \ WebAdminPage.cs行:989 – 无论如何我可以解决这个问题,还是我必须重做我的整个项目?(希望不是因为有很多工作要做。) 我是故障排除的新手,所以一步一步的指南将不胜感激。 谢谢!

以编程方式访问web.config的部分?

有没有办法访问web.config文件中的标签? 我想检查文件中的“ debug ”属性是否设置为“ true ”,但我似乎无法弄清楚如何做到这一点。 我已经尝试过使用WebConfigurationManager ,但这似乎不允许我进入部分。 更新: 我知道我可以轻松地像XML文档一样加载文件并使用XPath,但我希望框架中已经存在一些可以为我做这件事的东西。 似乎会有一些东西,因为已经有方法来获取应用程序设置和连接字符串。 我也尝试过以下几种方式使用WebConfigurationManager.GetSection()方法: WebConfigurationManager.GetSection(“compilation”)// Name of the tag in the file WebConfigurationManager.GetSection(“CompilationSection”) // Name of the class that I’d expect would be returned by this method WebConfigurationManager.GetSection(“system.web”) // Parent tag of the ‘compilation’ tag 所有上述方法都返回null 。 我假设有一种方法可以进入这个配置部分,因为有一个已经存在的类(’ CompilationSection ‘),我只是想不通如何得到它。

我无法写入配置文件

我正在使用这个类写入配置文件。 代码已构建且应用程序启动,但每次检查app.config时,我都看不到任何内容。 可能是什么问题呢? 这是代码: public class ConfigSettings { private ConfigSettings() { } public static string ReadSetting(string key) { return ConfigurationManager.AppSettings[key]; } public static void WriteSetting(string key, string value) { // load config document for current assembly XmlDocument doc = loadConfigDocument(); // retrieve appSettings node XmlNode node = doc.SelectSingleNode(“//appSettings”); if (node == null) throw new InvalidOperationException(“appSettings […]

在外部文件中编写appSettings

我有一个配置文件app.exe.config和appSettings部分有这样的: app.file.config文件有这样的东西: 我需要在运行时编辑var1,var2和var3,我有这样的代码: Configuration config = ConfigurationManager.OpenExeConfiguration(“…path\app.exe); config.AppSettings.SectionInformation.ConfigSource = “app.file.config”; config.AppSettings.Settings[“var1”].Value = “value 11”; config.AppSettings.Settings[“var2”].Value = “value 22”; config.AppSettings.Settings[“var3”].Value = “value 33”; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(“appSettings”); 当我运行config.Save ….文件app.file.config有一个appSettings节点,其属性为“file”。 此属性具有app.file.config的值 现在,如果我尝试加载配置文件,我有一个例外,消息“无法识别的属性’文件’。请注意,属性名称区分大小写。” 在app.file.config中。 如果手动删除文件属性,则会正确加载配置文件。 有任何想法吗? 保存配置文件时如何避免写入文件属性。 谢谢