首页 > 其他分享 >wpf write value to config file and read the persisted value

wpf write value to config file and read the persisted value

时间:2024-03-29 12:11:35浏览次数:13  
标签:ConfigurationManager read System value write Windows using config

<Window x:Class="WpfApp26.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp26"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" HorizontalAlignment="Left" Content="Load Folder" FontSize="30" Click="LoadFolderCmd" />
        <RichTextBox x:Name="rtb" Grid.Row="1" />
    </Grid>
</Window>


//

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace WpfApp26
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }

        void ReadValueFromConfig()
        {
            StringBuilder builder = new StringBuilder();
            foreach(string config in ConfigurationManager.AppSettings)
            {
                builder.AppendLine($"key:{config},value:{ConfigurationManager.AppSettings[config]}");
            } 
            rtb.AppendText( builder.ToString() );
        }

        void WriteValueToConfig(int len)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            for(int i=0;i<len;i++)
            {
                config.AppSettings.Settings.Add(Guid.NewGuid().ToString(), $"{DateTime.Now.ToString("O")}_{Guid.NewGuid().ToString()}");

                // Save the changes in App.config file.
                config.Save(ConfigurationSaveMode.Modified);

                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");
            } 
        }

        private void LoadFolderCmd(object sender, RoutedEventArgs e)
        {
            WriteValueToConfig(10);
            ReadValueFromConfig();
        }
    }
}
config.AppSettings.Settings.Add(Guid.NewGuid().ToString(), $"{DateTime.Now.ToString("O")}_{Guid.NewGuid().ToString()}");

                // Save the changes in App.config file.
                config.Save(ConfigurationSaveMode.Modified);

                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");

 

 

 

 

标签:ConfigurationManager,read,System,value,write,Windows,using,config
From: https://www.cnblogs.com/Fred1987/p/18103549

相关文章

  • 泛微e-cology_getE9DevelopAllNameValue2任意文件读取漏洞
    漏洞描述泛微e-cology依托全新的设计理念,全新的管理思想。为中大型组织创建全新的高效协同办公环境。智能语音办公,简化软件操作界面。身份认证、电子签名、电子签章、数据存证让合同全程数字化。泛微e-cologygetE9DevelopAllNameValue2接口存在任意文件读取漏洞,通过该漏洞......
  • [Paper Reading] LVM: Sequential Modeling Enables Scalable Learning for Large Vis
    LVM:SequentialModelingEnablesScalableLearningforLargeVisionModelsLVM:SequentialModelingEnablesScalableLearningforLargeVisionModels时间:23.12机构:UCBerkeley&&JohnsHopkinsUniversityTL;DR本文提出一种称为大视觉模型(LVM)的方法,该方法以"vis......
  • 3D Object Detection Essay Reading 2024.03.27
    PointTransformerV3:Simpler,Faster,Strongerpublish:CVPR2024paper:https://arxiv.org/abs/2312.10035code:https://github.com/Pointcept/PointTransformerV3commentary:https://zhuanlan.zhihu.com/p/673760352idea:​ 作者在3Dlarge-scale表示学习中认识到模型......
  • ThreadPool-线程池使用及原理
    1.线程池使用方式示例代码://一池N线程Executors.newFixedThreadPool(int)//一个任务一个任务执行,一池一线程Executors.newSingleThreadExecutorO//线程池根据需求创建线程,可扩容,遇强则强Executors.newCachedThreadPool()//自定义线程池方式newThreadPoolExec......
  • 创建与启动线程之二(继承Thread类)(实现Runnable接口)
    1.概述java的JVM允许程序运行多个线程.使用java.lang.Thread来表示线程.一个线程都直接或间接的继承于Thread类,即每个线程的对象要么是Thread的实例,要么是其子类的实例.2.Thread类的特性每个线程都是通过某个特定的Thread对象的run方法来完成操作的,run()被称为线程执行体.......
  • 解决“AttributeError: ‘numpy.ndarray’ object has no attribute ‘value_counts’
    成功解决AttributeError:‘numpy.ndarray’objecthasnoattribute‘value_counts’大家好,今天我想分享一个我在Python编程过程中遇到的问题,并详细阐述我是如何解决的。这个问题是关于numpy.ndarray对象没有value_counts属性的AttributeError。一、问题背景与错误描述......
  • could not read Username for 'https://github.com': terminal prompts disabled on w
    https://stackoverflow.com/questions/75990435/could-not-read-username-for-https-github-com-terminal-prompts-disabled-on-w  TrysettingatemporarycredentialhandlerforGitHub:GIT_USER="your-github-username-or-email"GIT_PASS="PAT"......
  • Key-N-Value--基于Protocol Buffers的树型协议处理引擎
    导言KNV是Key-Value协议的无限嵌套和模式自由的扩展,允许使用者快速访问或修改ProtoBuffers协议中的一部分或者多个部分,KNV原是一个面向对象缓存系统的一部分,后面作为腾讯第一批开源组件对外开源。KNV的理念也申请并获得国家专利。​​​​​​​项目地址:GitHub-shaneyuee/......
  • kindle电子书_越狱后koreader中自带OCR功能的设置
    KOReader一直以PDF阅读的优秀体验而闻名。然而,在尚未配置的情况下阅读图片类PDF在KOReader中长按屏幕无法像原生系统阅读正常文档一样划线取词,并且此时KOReader会弹出一个弹窗:没有OCR识别结果,或没有语言数据。KOReader有一个内置的OCR引擎用于识别扫描的PDF和DjVu文档中的文字......
  • 国内丐丐版overleaf--Write Papers Easy beta版来了
    引言此刻,今年将要毕业的无数莘莘学子正在奋笔疾书写着自己的毕业论文,淹没于繁琐的论文排版格式中。小编也是从这个时候走过来的,深知其中不易之处。好在现在有overleaf平台,可以稍稍缓解我们的痛点。但是经过一些调研发现,仍然存在两大痛点:痛点1:没有对应的LaTeX模板痛点2:overle......