【问题标题】:User.config corruptionUser.config 损坏
【发布时间】:2017-07-31 05:40:35
【问题描述】:

所以我已经做了相当多的研究来试图解决这个问题,但似乎无法 1)重现问题,但更重要的是 2)找到一个最新的解决方案来修复它。

在 2 周内已经发生过两次 user.config 文件随机损坏(例如,缺少 XML 文件块),从而导致下次启动时应用程序崩溃。

悬崖笔记:

  1. 我需要一个最新的解决方案来以编程方式处理此问题(即无论是来自 CodeProject Handling Of Corrupted User Config 的帖子还是其他帖子

  2. 我还需要一个可重现的案例来“破坏”user.config 文件(除了删除其中的部分,因为它对我不起作用)才能测试上面的 #1。

我遇到的解决方案:

  1. 我知道您可以手动导航到疯狂路径 (.../AppData/..././1.0.0.0/user.config) 并将其删除并重新运行应用程序,但是,这对我们来说不是一个合适的解决方案。我们需要能够以编程方式捕获这个错误,并相应地处理它。如果可能,最好不必重新启动应用程序。

  2. 我发现了一些帖子/答案,它们与本文的版本略有不同CodeProject Handling Of Corrupted User Config 他们使用与该链接相同的机制,或使用 ConfigurationManager 命令打开配置文件并捕获异常。

我尝试过的:

  1. 我的一个问题是我似乎无法访问 C# 4.5.2 中的 ConfigurationManager 类。但是 MSDN 文档说它在当前框架中可用。所以我真的无法尝试

  2. 当我尝试手动“损坏”我的 user.config 文件来测试我的解决方案时,它没有给我带来任何问题,并且应用程序运行良好...这令人担忧,因为我找不到可重现的测试给定解决方案是否有效的案例(即上面链接中提供的不使用 ConfigurationManager 的解决方案)

任何帮助将不胜感激!

【问题讨论】:

  • 有人有任何线索或提示可以提供帮助吗?
  • 放一些二进制文件(如 .dotx 文件)以重现问题
  • 同目录下的新文件一样吗?它不是“仅”搜索 user.config 吗?或者我错过了什么?
  • 当然你得把它命名为“user.config”
  • 我也有同样的问题。有时在电脑关机时。

标签: c# .net wpf crash settings


【解决方案1】:

我需要一个不会丢失所有设置的更强大的解决方案 我的解决方案是成功测试以创建备份。在任何后续失败时,都可以使用最后一次备份。退出应用程序可能会刷新备份。

            string configPathBackup;
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
                configPathBackup = config.FilePath + ".bak";
                config.SaveAs(configPathBackup, ConfigurationSaveMode.Full, true);
            }
            catch (ConfigurationErrorsException ex)
            {
                string filename = ex.Filename;
                configPathBackup = filename + ".bak";
                //_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);

                    if (!string.IsNullOrEmpty(configPathBackup) && File.Exists(configPathBackup))
                    {
                        File.Copy(configPathBackup, filename, true);
                    }

                }
                Kinesis.Properties.Settings.Default.Reload();
                //_logger.Error("Config file {0} does not exist", filename);
            }

【讨论】:

    【解决方案2】:

    要手动损坏您的文件,只需删除其中的所有文本并保存文件即可。

    为了访问ConfigurationManager 类,您必须添加对System.Configuration 的DLL 引用。如果您在添加参考时需要帮助,这些是列出的步骤online

    1. 在解决方案资源管理器中,右键单击项目节点,然后单击添加引用。
    2. 在“添加引用”对话框中,选择指示要引用的组件类型的选项卡。
    3. 选择要引用的组件,然后单击“确定”。

    要解决损坏问题,您可以使用如下所示的 try/catch 块:

    try {
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    }
    catch (ConfigurationErrorsException exception) {
        Console.WriteLine("Settings are Corrupt!!");
        // handle error or tell user
        // to manually delete the file, you can do the following:
        File.Delete(exception.Filename); // this can throw an exception too, so be wary!
    }
    

    编辑:进一步说明,请注意,如果您在任何地方都有Settings.Upgrade() 呼叫并且您的“旧”文件已损坏,这也会导致软件崩溃。确保在您的 Settings.Upgrade() 呼叫周围也加上 try/catch 块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-05
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多