zoukankan      html  css  js  c++  java
  • App.config 动态编辑

    using System.Configuration

    App.config

    Configuration _config;
    string _paraPath = "Path";
    
    public MainWindow()
    {
        InitializeComponent();
        this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        InitConfig();
        // E:Repository****.csproj
        string filePath = _config.AppSettings.Settings[_paraPath].Value;
        GetData(filePath);
    }
    private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
    {
        _config.AppSettings.Settings[_paraPath].Value = txtDir.Text;
        _config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
    
    private void InitConfig()
    {
        try
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            Assembly assembly = Assembly.GetCallingAssembly();
            Uri uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
            string configName = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".exe.config");
            if (!File.Exists(configName))
            {
                XmlDocument xml = new XmlDocument();
                xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
                xml.AppendChild(xml.CreateElement("configuration"));
                xml.Save(configName);
                map.ExeConfigFilename = configName;
                _config = ConfigurationManager.OpenMappedExeConfiguration(map, 0);
                _config.AppSettings.Settings.Add(_paraPath, @"E:Repository****.csproj");
            }
            else
            {
                map.ExeConfigFilename = configName;
                _config = ConfigurationManager.OpenMappedExeConfiguration(map, 0);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    private void GetData(string filePath)
    {
        txtDir.Text = filePath;
    }
    
    

    app.cs

    (Application.Current as App).SetAppSettings();
    
  • 相关阅读:
    linux shell 脚本30分钟教程
    ubuntu nginx+mysql+php 服务器环境自动配置脚本
    前端开发中常用工具函数总结
    经常逛的技术网站
    简单好用的在线思维导图工具
    在线短信接收
    一些图片站
    常用CSS媒体查询
    Dart Language samples
    IDEA 快捷键
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/12566202.html
Copyright © 2011-2022 走看看