首页 / C# / C#-部署后缺少配置文件
C#-部署后缺少配置文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-部署后缺少配置文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3674字,纯文字阅读大概需要6分钟。
内容图文
更新:我在下面有一个问题,但实际上我的问题可以通过提出一个稍微不同的问题来解决.为什么在某些计算机上我的应用程序会引发错误:
Configuration system failed to initialize - System.Configuration - at System.Configuration.ConfigurationManager.PrepareConfigSystem()
在其他机器上没有的地方.如此处.NET 3.5 – Configuration system failed to initialize exception所述,该错误是由我的app.config顶部缺少configSections元素引起的.当然,可以通过放置此部分来解决该问题,但是由于某些原因,在我的项目解决方案中具有该部分的app.config并不是一旦部署就在appdata文件夹中创建的部分.
原始问题:
在某些计算机上而不是其他计算机上部署时,为什么我的用户配置文件会缺少此部分?我如何确保它不丢失.
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="NameOfAddin_Add_in.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
一些背景.我正在通过单击一次Visual Studio安装程序在运行Outlook 2007和2010的Win 7计算机上部署一次vsto加载项.
加载项读取一些设置并将这些设置写入app.config文件,这与exe不同,该设置存储在本地用户的appdata文件夹中.
但是在某些计算机上,我收到错误消息“配置系统无法初始化-System.Configuration-位于System.Configuration.ConfigurationManager.PrepareConfigSystem()”
在我看来,这是由xml中上述缺少的元素引起的.但是,在其他计算机上,configSections也不缺少.该问题与所使用的Outlook版本无关.
解决方法:
我昨天在VSTO DLL项目中遇到了相同的问题,但我仍然不明白为什么有时会丢失带有name =“ userSettings”的原因.
但我可以提供解决方案:我已经制作了一个函数,该函数将从固定的“ .dll.config”文件中丢失的XML部分(如果缺少)复制到ROAMING目录中的配置文件中:
/// <summary>
/// Corrects the roaming settings file if needed because sometimes the node "configSections" is missing in the settings file.
/// Correct this by taking this node out of the default config file.
/// </summary>
private static void CorrectRoamingSettingsFileIfNeeded()
{
const string NODE_NAME_CONFIGURATION = "configuration";
const string NODE_NAME_CONFIGSECTIONS = "configSections";
const string NODE_NAME_USERSETTINGS = "userSettings";
const string ADDIN_DLL_FILENAME = "MyAddIn.dll";
//Exit if no romaing config (file) to correct...
var configRoaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
if (configRoaming == null) return;
if (!configRoaming.HasFile) return;
//Check for the <sectionGroup> with name="userSettings"
//Note: Used ugly iteration because "configRoaming.GetSectionGroup(sectionGroupName)" throws ArgumentException.
ConfigurationSectionGroup sectionGroupUserSettings = null;
foreach (ConfigurationSectionGroup sectionGroup in configRoaming.SectionGroups)
{
if (sectionGroup.Name.Equals(NODE_NAME_USERSETTINGS))
{
sectionGroupUserSettings = sectionGroup;
break;
}
}
//Exit if the needed section group is found...
if (sectionGroupUserSettings != null && sectionGroupUserSettings.IsDeclared) return;
//Do correction actions...
var xDoc = XDocument.Load(configRoaming.FilePath);
var userSettingsNode = xDoc.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_USERSETTINGS);
var addInDllConfigFullFilename = AppDomain.CurrentDomain.BaseDirectory + ADDIN_DLL_FILENAME;
var configDefault = ConfigurationManager.OpenExeConfiguration(addInDllConfigFullFilename);
var xDocDefault = XDocument.Load(configDefault.FilePath);
var configSectionsNode = xDocDefault.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_CONFIGSECTIONS);
userSettingsNode.AddBeforeSelf(configSectionsNode);
xDoc.Save(configRoaming.FilePath);
}
内容总结
以上是互联网集市为您收集整理的C#-部署后缺少配置文件全部内容,希望文章能够帮你解决C#-部署后缺少配置文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。