我的问题很简单。我有一个第三方库,它假设它将在网站中使用(需要在web.config中的特殊配置部分)。我想在C#控制台应用程序中使用这个库。有可能吗?
我尝试过将所有内容复制到app.config中,但WebConfigurationManager.OpenWebConfiguration();不起作用。我收到的错误是一个说明An error occurred creating the configuration section handler for ...: Could not load type 'Configuration.RenderingSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.的ConfigurationErrorsException
发布于 2014-06-26 05:24:24
您可以通过AppDomain.CurrentDomain.SetData(string name, Object data)设置APP_CONFIG_FILE。
string webconfigPath = "whatever......../web.config");
string webconfigDir = Path.GetDirectoryName(configPath);
AppDomain.CurrentDomain.SetData("APPBASE", webconfigDir);
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", webconfigPath);它很简单,您将获得控制台应用程序(cron)和web应用程序相同设置。
发布于 2012-11-13 01:05:56
请改用app.config。
与web.config完全相同的语法、选项等,但适用于控制台和WinForms应用程序。
另外,听着,here,我还没有测试过它,但它似乎对这个人很有效。但是,如果您的项目与web无关,我仍然建议您使用app.config。
https://stackoverflow.com/questions/13348016
复制相似问题