首页 > 其他分享 >user.config文件的备份和恢复(winform)

user.config文件的备份和恢复(winform)

时间:2024-05-28 22:44:39浏览次数:18  
标签:文件 备份 filename user winform config configPathBackup

user.config文件的备份和恢复(winform)

场景
出现user.config文件内容破坏,全为0x00的问题。

思路
备份和恢复。启动时,如果user.config文件有效,则备份到备份文件,如果无效,则恢复备份文件到user.config文件。

文件备份和恢复函数(Program.cs文件)
private static bool MonitorConfigFileIsDamage()
{
    string configPathBackup;
    try
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);

        configPathBackup = config.FilePath + ".bak";

        config.SaveAs(configPathBackup, ConfigurationSaveMode.Full, true);

        return true;
    }
    catch (ConfigurationErrorsException ex)
    {
        string filename = ex.Filename;
        configPathBackup = filename + ".bak";
        if (File.Exists(filename) == true)
        {
            File.Delete(filename);
            if (!string.IsNullOrEmpty(configPathBackup) && File.Exists(configPathBackup))
            {
                File.Copy(configPathBackup, filename, true);
            }
        }
        Settings.Default.Reload();

        return false;
    }
}
调用文件备份和恢复函数(Program.cs文件)
[STAThread]
static void Main(string[] args)
{
    if (MonitorConfigFileIsDamage() == false)
    {
        Application.Restart();
        return;
    }
}

标签:文件,备份,filename,user,winform,config,configPathBackup
From: https://www.cnblogs.com/epicblue/p/18215065

相关文章

  • React中何时使用memo、useCallback、useMemo以及useRef进行性能优化
    react无法做到像vue一样自动收集依赖更新(期待react19的ReactCompiler),需要开发人员手动的进行性能优化,此时memo、useCallback、useMemo、useRef就是性能优化中的重要API本文虽然介绍可应用场景,但是正常开发中,尤其是useCallback。除非遇到性能问题或者组件库封装,亦或......
  • VS2022查看项目宏定义(SolutionDir/Configuration/ProjectName等)
    参考:https://blog.csdn.net/aoxuestudy/article/details/122197793右击打开C++项目属性点击【编辑】:点击【宏】:进一步搜索定位:......
  • ifconfig命令得到的信息含义
    ens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet192.168.88.132netmask255.255.255.0broadcast192.168.88.255inet6fe80::aedc:d547:d9e0:797cprefixlen64scopeid0x20ether00:0c:29:19:65:b9txqueuelen1000(以太网)RXpackets6......
  • 通过vconfig命令配置VLAN
    #yuminstallvconfig 安装vconfig命令包#modprobe8021q加载8021q模块,若不支持8021q模块,则不支持VLAN#lsmod|grep-i8021q #vconfigaddeth0100 在eth0上配置VLAN100#vconfigaddeth0200 在eth0上配置VLAN200#vconfigset_flageth0.10011设置VLAN的REO......
  • Preserve Built-in and User-Defined Styles as CSS
    PreserveBuilt-inandUser-DefinedStylesasCSSGroupDocs.EditorforJava24.4addstheabilitytopreserveandstorebuilt-inanduser-definedstylesasCSSrulesetsinanexternalstylesheet.GroupDocs.EditorforJavaisadocumenteditingl......
  • HITSC_3_Software Construction Process and Configuration Management
    目标软件开发流程及模式,敏捷开发,软件配置管理SCM,Git,软件构造过程和构造工具SDLC传统软件开发模型瀑布模型线性模型优点:划分阶段,管理简单缺点:不迭代,所以缺少灵活性,也难以适应需求;并且用户看不到原型,导致风险高,前期错误后期发现增量模型瀑布串行,容易适应需求增加V模型......
  • X2Modbus网关GetUser接口存在信息泄漏漏洞
    漏洞描述该漏洞的存在是由于'GetUser'接口在处理SOAP请求时,缺乏对请求的充分验证和授权检查,任何人都可以通过发送特定的SOAP请求,获取系统中存在的用户信息,攻击者可以通过发送包含合法用户凭据的SOAP请求,绕过身份验证机制,直接获取用户数据。fofaserver="SunFull-Webs"POCPOST......
  • Spring 框架类PropertySourcesPlaceholderConfigurer
    PropertyOverrideConfigurer是Spring框架中的一个类,它允许你在Spring的配置文件之外通过外部属性文件来覆盖已定义的bean属性。这在部署不同的环境(如开发、测试、生产)时特别有用,因为你可以为不同的环境定义不同的属性,而无需修改Spring的配置文件。演示:创建实体类:p......
  • 使用@ConfigurationProperties注解产生的一个小错误
    今天在写项目时碰到一个错误:Notregisteredvia@EnableConfigurationProperties,markedasSpringcomponent,orscannedvia@ConfigurationPropertiesScan原代码如下:@Data@ConfigurationProperties(prefix="catshop.auth")publicclassAuthProperties{//相关......
  • The configuration for MySQL Server 8.0.27 has failed You can find more informati
    遇见这种情况,作者当时也是痛苦万分,网上找了许许多多的方法试了好多次都不行。分析问题出现这种问题是因为我们之前安装过但是没有安装完全就取消了,电脑里面已经存储了。重新安装的时候把安装位置和数据存放的位置路径全部使用英文,例如:之前我的安装路径:D:\用户\app\mysql......