<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