13

標準の.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ファイルのフルパスを取得する方法を知っている人はいますか?

4

3 に答える 3

16

これは、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;
    }

基本的に、Sittingsに依存してエラーをスローするのではなく、ConfigurationManagerを使用してファイルを読み取ります。そうすれば、システムのバージョンが悪い状態になることはありません。

于 2013-09-19T22:12:39.847 に答える
11

プログラムで回復する方法は、手動で行ったことを実行することです。ユーザー設定ファイルを削除します。 次に、を呼び出しますSettings.Reset。(削除する代わりに、デフォルト値を使用して新しいユーザー設定ファイルを作成することもできますが、構成マネージャーを適切に使用している場合は、基本的に同じです。)

これは非常にまれな出来事ですが、まったく前例のないことではありません。ユーザー設定ファイルの書き込み中にプログラムがクラッシュする可能性があるだけでなく、ファイル自体がユーザー書き込み可能であるため、ユーザーが実行する他のプログラムがファイルを混乱させる可能性があります。

この特定の脆弱性を回避するには、トランザクションの整合性を備えた永続的なストア、つまりデータベースにユーザー設定を保持します。(これだけでなく、まだ脆弱性があります。)これは、ほとんどの場合、信頼性のわずかな改善になるための多くの作業です。しかし、「ほとんどの場合」は「すべての場合」を意味するわけではありません。あなたはそれを正当化するかもしれません。

于 2010-02-15T23:54:27.117 に答える
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();
    }
}
于 2011-09-20T22:57:40.053 に答える