我在c#windows应用程序中遇到此错误:“配置系统无法初始化”。
工作正常。突然间我得到了这个例外。它显示内部异常细节为“缺少根元素”。 (C:\ Users \ company \ AppData \ Local \ Clickbase_Corp_Sverige_AB \ TouchStation.vshost.exe_Url_no1nets4fg3oy2p2q2pnwgulbvczlv33 \ 1.1.0.12 \ user.config)“}。当我尝试从Settings.cs类中获取值时会发生这种情况。
在program.cs文件中,编写以下代码
if (Properties.Settings.Default.CallUpgrade)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.CallUpgrade = false;
Properties.Settings.Default.Save();
}
并调用settings.cs类,其中以下代码抛出异常
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CallUpgrade {
get {
return ((bool)(this["CallUpgrade"]));
}
set {
this["CallUpgrade"] = value;
}
}
以下是我的整个app.config
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="TouchStation.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<section name="SitesInfo" type="TouchServer.SitesInfoSectionHandler,TouchServerLib" />
</configSections>
<appSettings>
<add key="WebRoot" value="webroot" />
<add key="TempDir" value="temp" />
<add key="ServerPort" value="9338" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<userSettings>
<TouchStation.Properties.Settings>
<setting name="Site" serializeAs="String">
<value />
</setting>
<setting name="StationID" serializeAs="String">
<value>0</value>
</setting>
<setting name="Location" serializeAs="String">
<value />
</setting>
<setting name="ShutdownTime" serializeAs="String">
<value>0000</value>
</setting>
<setting name="ReportStatusEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="SynchronizeEvery" serializeAs="String">
<value>10</value>
</setting>
<setting name="DefaultUsername" serializeAs="String">
<value />
</setting>
<setting name="DefaultPassword" serializeAs="String">
<value />
</setting>
<setting name="WatchdogTimeout" serializeAs="String">
<value>60</value>
</setting>
<setting name="RebootOnTimeout" serializeAs="String">
<value>False</value>
</setting>
<setting name="AnonymousLogin" serializeAs="String">
<value>True</value>
</setting>
<setting name="RefID" serializeAs="String">
<value />
</setting>
<setting name="AutoStart" serializeAs="String">
<value>False</value>
</setting>
<setting name="DemoMode" serializeAs="String">
<value>True</value>
</setting>
<setting name="UnlockPassword" serializeAs="String">
<value>needle</value>
</setting>
<setting name="SynchronizerUsername" serializeAs="String">
<value />
</setting>
<setting name="SynchronizerPassword" serializeAs="String">
<value />
</setting>
<setting name="RunClientApplications" serializeAs="String">
<value>False</value>
</setting>
<setting name="MapID" serializeAs="String">
<value>0</value>
</setting>
<setting name="ServerName" serializeAs="String">
<value />
</setting>
<setting name="CallUpgrade" serializeAs="String">
<value>True</value>
</setting>
<setting name="ServerPort" serializeAs="String">
<value>9338</value>
</setting>
</TouchStation.Properties.Settings>
<TouchStation.TouchStation>
<setting name="ServerURL" serializeAs="String">
<value />
</setting>
<setting name="Site" serializeAs="String">
<value />
</setting>
<setting name="StationID" serializeAs="String">
<value>0</value>
</setting>
<setting name="Location" serializeAs="String">
<value />
</setting>
<setting name="ShutdownTime" serializeAs="String">
<value />
</setting>
<setting name="ReportStatusEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="SynchronizeEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="HideMouse" serializeAs="String">
<value>False</value>
</setting>
<setting name="HideDesktopOnStart" serializeAs="String">
<value>False</value>
</setting>
<setting name="DefaultUsername" serializeAs="String">
<value />
</setting>
<setting name="DefaultPassword" serializeAs="String">
<value />
</setting>
<setting name="LogServerPort" serializeAs="String">
<value>9050</value>
</setting>
<setting name="WatchdogTimeout" serializeAs="String">
<value>60</value>
</setting>
<setting name="RebootOnTimeout" serializeAs="String">
<value>False</value>
</setting>
<setting name="AnonymousLogin" serializeAs="String">
<value>True</value>
</setting>
<setting name="RefID" serializeAs="String">
<value />
</setting>
</TouchStation.TouchStation>
</userSettings>
<applicationSettings>
<TouchStation.TouchStation>
<setting name="ClientSettingsURL" serializeAs="String">
<value />
</setting>
</TouchStation.TouchStation>
</applicationSettings>
<SitesInfo>
<sites>
<site Name="Local" FullName="Local Site" DatabaseConnectionString="Data\local.db" />
</sites>
</SitesInfo>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
任何人都可以帮助我吗?
谢谢。
此致
珍
答案 0 :(得分:7)
除了Akram Shahda的回答:我遇到了同样的问题(配置系统初始化失败/ root元素丢失)。 AppData文件夹中的.config文件为空。通过删除AppData文件夹中的.config文件解决了该问题。
答案 1 :(得分:5)
缺少XmlException
授权的Root元素的原因意味着您尝试加载的XML文档(此处的配置文件)格式不正确,更准确地说它缺少根节点。
每个XML文件必须有一个根元素/节点,它包含所有其他元素。
您的文件必须如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<userSettings>
<WindowsFormsApplication.Properties.Settings>
</WindowsFormsApplication.Properties.Settings>
</userSettings>
</configuration>
答案 2 :(得分:2)
在控制台应用中,你应该分配&lt;启动/&GT;之后的部分&lt; configSections /&GT;
答案 3 :(得分:2)
以防任何人阅读This:
对我来说,我只是转到user.config位置解决了这个问题,对于这个问题它是:
(C:\用户\公司\应用程序数据\本地\ Clickbase_Corp_Sverige_AB \ TouchStation.vshost.exe_Url_no1nets4fg3oy2p2q2pnwgulbvczlv33 \ 1.1.0.12 \ user.config)“
我删除了配置文件并重新启动了visual studio并且它工作正常。
希望这有帮助!
由于
答案 4 :(得分:1)
通常,“配置系统初始化失败”问题可能是由app.config中的无效XML结构引起的。当我试图将代码从“测试床”配置文件移植到服务app.config文件时,我偶然发现了这一点。
答案 5 :(得分:1)
在我的例子中,connectionStrings节点被声明了两次。
答案 6 :(得分:0)
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
con = new OleDbConnection(config.ConnectionStrings.ConnectionStrings["connectionStr"].ConnectionString.ToString());
答案 7 :(得分:0)
我们的一位客户收到此错误&#34;配置系统无法初始化&#34;尝试从C#项目FTP时。用户不是管理员。
我们的技术客户调查并发现,在非管理员帐户下使用ProcMon,此进程使用的资源是.NET machine.config文件。 (C:\ WINDOWS \ microsoft.net \框架\ v4.0.30319 \ CONFIG \ machine.config中)
我们的客户授予本地用户修改此配置文件的权限,并立即解决了该问题。 (他们不允许用户成为管理员,因此这是必要的。)
因此,如果上述答案对你不起作用,也许这会有所帮助。
答案 8 :(得分:0)
就我而言,在检查完所有问题之后,问题发生在App.config的最后一部分,其中一个Log4Net配置语句产生了错误。
删除App.config中的所有额外内容并开始调试。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="[Application Name].Settings.Client" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<applicationSettings>
<[Application Name].Settings.Client>
<setting name="IDClient" serializeAs="String">
<value>6</value>
</setting>
</[Application Name].Settings.Client>
</applicationSettings>
</configuration>
答案 9 :(得分:0)
使用Graphviz的.Net Core:
<configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > </sectionGroup> </configSections> <appSettings> <add key="graphVizLocation" value="C:\Program Files (x86)\Graphviz2.38\bin" /> </appSettings> </configuration>