2012-03-05 24 views
17

我注意到在一些機器中,我的應用程序的user.config文件在某種程度上被損壞,並且在打開時爲空。我似乎無法弄清楚爲什麼會發生這種情況。有沒有共同的事情會導致這種情況?任何方式來安全地防止這一點?是什麼導致user.config爲空?如何在不重新啓動的情況下恢復?

我的第二個問題是如何恢復狀態?我發現異常並刪除了user.config文件,但找不到重新啓動應用程序的情況下恢復配置的方法。一切我Properties對象上做導致以下錯誤:

「配置系統初始化失敗」

復位,刷新和升級都無助於解決問題。

這裏是我的異常後刪除代碼:

catch (System.Configuration.ConfigurationErrorsException ex) 
{ 
    string fileName = ""; 
    if (!string.IsNullOrEmpty(ex.Filename)) 
     fileName = ex.Filename; 
    else 
    { 
     System.Configuration.ConfigurationErrorsException innerException = ex.InnerException as System.Configuration.ConfigurationErrorsException; 
     if (innerException != null && !string.IsNullOrEmpty(innerException.Filename)) 
      fileName = innerException.Filename; 
    } 
    if (System.IO.File.Exists(fileName)) 
     System.IO.File.Delete(fileName); 
} 
+2

如果user.config文件corrrupt,你必須重新啓動,只要你實際存儲設置的文件中值。您必須重新啓動的原因很明顯,如果文件損壞,您的應用程序處於不穩定狀態。 – 2012-03-05 18:57:07

+1

這就是我想的。我希望有一種方法可以在運行時將應用程序配置恢復到默認設置而無需重啓 - 因爲重啓看起來非常不可靠。 – JeremyK 2012-03-05 19:00:25

+1

我有一個應用程序不時出現同樣的問題。迄今爲止,我還沒有找到解決這個問題或解決這個問題的根本原因。該應用程序已從.NET v3.5移至v4.0,因此該問題在v2.0和v4.0運行時均存在。可能的重複:http://stackoverflow.com/questions/3071651/how-to-catch-exception-when-loading-net-winform-app-user-config-file – harlam357 2013-02-25 14:47:16

回答

13

我們在應用程序有這個問題 - 我無法找出原因(我的猜測是,我寫Properties.Settings太頻繁,但我不知道)。無論如何,我的解決方法是在下面。關鍵是要刪除已損壞的文件,並調用Properties.Settings.Default.Upgrade()

try 
{ 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
} 
catch (ConfigurationErrorsException ex) 
{ 
    string filename = ex.Filename; 
    _logger.Error(ex, "Cannot open config file"); 

    if (File.Exists(filename) == true) 
    { 
     _logger.Error("Config file {0} content:\n{1}", filename, File.ReadAllText(filename)); 
     File.Delete(filename); 
     _logger.Error("Config file deleted"); 
     Properties.Settings.Default.Upgrade(); 
     // Properties.Settings.Default.Reload(); 
     // you could optionally restart the app instead 
    } 
    else 
    { 
     _logger.Error("Config file {0} does not exist", filename); 
    } 
} 
+0

這似乎很有希望,但是當我運行升級()我得到配置系統初始化失敗。 – tofutim 2015-09-21 23:00:37

+0

@tofutim究竟是什麼意思?並可能嘗試刪除+重裝()調用,在第二個想法了。在你的應用程序沒有升級.. – avs099 2015-09-21 23:09:54

+2

實際上,這樣做的工作 - 但使用的,而不是要求一個糟糕的設置openexeconfiguration,呼籲之後,這一點很重要糟糕的設置,那麼你堅持與配置系統初始化失敗 – tofutim 2015-09-21 23:11:57

4

我有類似的情況。對我來說,一旦我刪除了錯誤的配置文件,我就讓應用程序繼續。下次訪問設置將使用應用程序默認值。

+1

如果我刪除配置並嘗試繼續,它仍然處於損壞狀態,並且每次使用配置設置時都會引發異常。配置系統無法初始化,因此試圖在不重新啓動的情況下使用它將不起作用。 – JeremyK 2012-05-09 19:30:08

+0

我在這裏也遇到同樣的問題。設置文件損壞後,無論調用reset()或reload()還是save()後,對settings屬性的訪問都會拋出異常。有可能以其他方式處理它。 – TrustyCoder 2014-10-02 16:00:51

4

這可能有點晚,但我已經做了一些更多的研究。 user.config文件似乎因未知原因而損壞,並且不會啓動應用程序。你可以在app.xaml.cs中放入一個小的try/catch邏輯,並檢查它何時啓動,以確保在源代碼中捕獲到這個問題。當應用程序啓動並嘗試以編程方式重新加載settings.default並失敗時,它將轉到給予用戶刪除文件的選項。

try { 
Settings.Default.Reload(); 
} 
catch (ConfigurationErrorsException ex) 
{ 
    string filename = ((ConfigurationErrorsException)ex.InnerException).Filename; 

if (MessageBox.Show("<ProgramName> has detected that your" + 
         " user settings file has become corrupted. " + 
         "This may be due to a crash or improper exiting" + 
         " of the program. <ProgramName> must reset your " + 
         "user settings in order to continue.\n\nClick" + 
         " Yes to reset your user settings and continue.\n\n" + 
         "Click No if you wish to attempt manual repair" + 
         " or to rescue information before proceeding.", 
         "Corrupt user settings", 
         MessageBoxButton.YesNo, 
         MessageBoxImage.Error) == MessageBoxResult.Yes) { 
    File.Delete(filename); 
    Settings.Default.Reload(); 
    // you could optionally restart the app instead 
} else 
    Process.GetCurrentProcess().Kill(); 
    // avoid the inevitable crash 
} 

信用 - http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings

希望這可以幫助別人:)

+2

'重新加載()'沒有幫助。下一次訪問'Default.Foo'將會拋出'ConfigurationErrorsException'。 – Martin 2015-02-19 12:38:13

+0

對不起,我不明白你的意思。重裝()函數是在try塊,一旦ConfigurationErrorsException被捉住,它會刪除該配置文件,並殺死應用。當wpf應用程序再次重新啓動時,它會自動創建一個空配置文件。這不是最好的,用戶將丟失存儲在配置文件中的任何信息,但至少應用程序不會完全無法使用。 – Arun 2015-03-12 09:20:25

+2

第二'Reload'不是try塊(但是這是不相關的),當用戶點擊「否」的應用程序將只被殺死。當用戶點擊「是」時,應用程序將繼續運行,並且下次訪問「Default.Foo」時將拋出異常。那麼,至少在我的電腦上... – Martin 2015-03-16 10:38:13

相關問題