我们在阅读标准的.Net用户设置时遇到了罕见的异常(这是VS 2008中“项目属性”中的设置):
System.Configuration.ConfigurationErrorsException was caught
Message="Configuration system failed to initialize"
Source="System.Configuration"
BareMessage="Configuration system failed to initialize"
Line=0
StackTrace:
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
at System.Diagnostics.DiagnosticsConfiguration.Initialize()
at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
at System.Diagnostics.TraceInternal.InitializeSettings()
at System.Diagnostics.TraceInternal.get_Listeners()
InnerException: System.Configuration.ConfigurationErrorsException
Message="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1. (C:\\Documents and Settings\\USER\\Local Settings\\Application Data\\Hitcents\\SettingsTest.vshost.exe_Url_ghwhc20utv4toanuinmj0pfsljthcugo\\1.0.0.0\\user.config line 7)"
Source="System.Configuration"
BareMessage="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1."
Filename="C:\\Documents and Settings\\USER\\Local Settings\\Application Data\\Hitcents\\SettingsTest.vshost.exe_Url_ghwhc20utv4toanuinmj0pfsljthcugo\\1.0.0.0\\user.config"
Line=7
StackTrace:
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
InnerException: System.Xml.XmlException
Message="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1."
Source="System.Xml"
LineNumber=7
LinePosition=1
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String arg)
at System.Xml.XmlTextReaderImpl.ThrowUnclosedElements()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlTextReaderImpl.Skip()
at System.Xml.XmlTextReader.Skip()
at System.Configuration.XmlUtil.StrictSkipToNextElement(ExceptionAction action)
at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
at System.Configuration.BaseConfigurationRecord.ScanSections(XmlUtil xmlUtil)
at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
InnerException:
*注意:这是从测试应用程序重新创建的。
我提取了user.config文件,其中一半文件丢失了。
我希望我们的申请因某种原因而突然终止。
这似乎非常罕见,以下是我们与设置互动的方式:
//How we read
Settings settings = Settings.Default;
_ourStaticMemberVariable = settings.OurValue;
//How we save
Settings settings = Settings.Default;
settings.OurValue = "Our Value";
settings.Save();
我们如何使用它有什么问题吗?两个调用都有一个try-catch,它放置了一些默认值,但是这些值需要能够从我们的应用程序中重置。
当处于这种状态时,我们的应用程序无法保存新设置 - 我无法找到以编程方式恢复的好方法。我必须手动找到user.config并将其删除。
我也试过调用Settings.Reset()等,但得到了相同的异常。
有关如何解决此问题的任何想法?或者我们最好是编写自己的设置系统还是以其他方式保存持久性设置?
编辑:如果遇到ConfigurationErrorsException,解决方法是从代码中删除文件。
任何人都知道如何获取user.config文件的完整路径?
答案 0 :(得分:14)
这是一个解决方案,不要求您退出应用程序并使用Jarle(http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings?msg=3608682#xx3608682xx)。早期,在调用“设置”之前,请使用此
public static bool CheckSettings()
{
var isReset = false;
try
{
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
catch (ConfigurationErrorsException ex)
{
string filename = string.Empty;
if (!string.IsNullOrEmpty(ex.Filename))
{
filename = ex.Filename;
}
else
{
var innerEx = ex.InnerException as ConfigurationErrorsException;
if (innerEx != null && !string.IsNullOrEmpty(innerEx.Filename))
{
filename = innerEx.Filename;
}
}
if (!string.IsNullOrEmpty(filename))
{
if (System.IO.File.Exists(filename))
{
var fileInfo = new System.IO.FileInfo(filename);
var watcher
= new System.IO.FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name);
System.IO.File.Delete(filename);
isReset = true;
if (System.IO.File.Exists(filename))
{
watcher.WaitForChanged(System.IO.WatcherChangeTypes.Deleted);
}
}
}
}
return isReset;
}
基本上,不是依赖于Sitting来抛出错误,而是使用ConfigurationManager读取文件,这样系统的版本就不会陷入糟糕的状态。
答案 1 :(得分:11)
以编程方式恢复的方法是执行您手动执行的操作 - 删除用户设置文件。 然后致电Settings.Reset
。 (您也可以使用默认值编写新的用户设置文件,而不是删除它,但如果您正确使用配置管理器,则基本上是相同的。)
这种情况非常罕见,但并非完全闻所未闻。在编写用户设置文件时,您的程序不仅会崩溃,文件本身也是用户可写的,因此用户运行的其他程序可能会弄乱它。
要避免此特定漏洞,请在具有事务完整性的持久存储中保留用户设置,即数据库。 (你仍然会有漏洞,而不是这个漏洞。)对于大多数情况下可靠性的微小改进而言,这是很多工作。但“在大多数情况下”并不意味着“在所有情况下”;你可以保证。
答案 2 :(得分:3)
[STAThread]
private static void Main(string[] args)
{
try
{
// ...
}
catch (System.Configuration.ConfigurationErrorsException ex)
{
var config = ((System.Configuration.ConfigurationErrorsException)ex.InnerException).Filename;
// notify user, ask them to restart
System.IO.File.Delete(config);
Application.Exit();
}
}