User.config损坏 [英] User.config corruption

查看:107
本文介绍了User.config损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在试图弄清楚这个问题上做了很多研究,但看起来似乎不是1)重现该问题,但更重要的是2)找到用于解决此问题的最新解决方案.

So I've done quite a bit of research on trying to figure this issue out but can't seem 1) Reproduce the issue, but more importantly 2) Find an update-to-date solution for fixing it.

在两周内,这已经发生过两次,其中user.config文件会随机损坏(例如,XML文件的一部分会丢失),从而导致下次启动时应用程序崩溃.

It's already happened twice within a span of 2 weeks where randomly the user.config file will get corrupted (e.g. there will be missing chunks of the XML file) and thus result in an application crash on next startup.

注释:

  1. 我需要最新的解决方案来以编程方式处理此问题(即,是否来自我还需要一种可重现的案例来破坏" user.config文件(除了删除其中的一部分,因为它对我不起作用),才能测试上面的#1.

    I also need a reproducible case for "corrupting" the user.config file (aside from deleting out chunks of it, as it did not work for me) to be able test #1 above.

    我遇到的解决方案:

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

    1. I know that you can manually navigate to the crazy path (.../AppData/..././1.0.0.0/user.config) and delete it and re-run the application, however, this is not an appropriate solution for us. We need to be able to programmatically catch this error, and deal with it accordingly. If possible, it'd be best to not have to restart the application.

    我发现了一些帖子/答案,与本文的版本稍有不同

    I've found a few posts/answers, that are slightly different versions of this article CodeProject Handling Of Corrupted User Config where they use either the same mechanism as that link, or use a ConfigurationManager command to open the config file and catch the exception.

    我尝试过的事情:

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

    1. One of my problems is that I cannot seem to access the ConfigurationManager class in C# 4.5.2. But MSDN docs say that it's available in the current framework. So I couldn't really try

    当我尝试手动破坏"我的user.config文件以测试我的解决方案时,它没有任何问题,并且该应用程序运行正常……这令人震惊,因为我找不到可复制的文件测试给定解决方案是否有效的案例(即,上面链接中提供的不使用ConfigurationManager的解决方案)

    When I try and manually "corrupt" my user.config file to test my solutions, it gives me no issues, and the application works fine...which is alarming, as I can't find a reproducible case to test whether or not the given solution works (i.e. the one provided in the link above that does NOT use ConfigurationManager)

    任何帮助将不胜感激!

    推荐答案

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

    To manually corrupt your file, just delete all of the text inside it and save the file.

    为了访问ConfigurationManager类,您必须向System.Configuration添加DLL引用.如果您需要添加参考的帮助,请按照以下步骤操作在线 :

    In order to access the ConfigurationManager class, you have to add a DLL reference to System.Configuration. If you need help adding a reference, these are the steps listed online:

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

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

    To account for the corruption, you can use a try/catch block like the following:

    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块.

    As a further note, note that if you have a Settings.Upgrade() call anywhere and your "old" file is corrupt, this will also crash the software. Make sure to surround your Settings.Upgrade() call with a try/catch block as well.

    这篇关于User.config损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆