Winform 读取和添加 appSetting节点

程序读写配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="section4" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="section3" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="section2" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="section1" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </configSections>
    <section4>
        <add key="section4key0" value="section4value0" />
        <add key="section4key1" value="section4value1" />
        <add key="section4key2" value="section4value2" />
    </section4>
    <section3>
        <add key="section3key0" value="section3value0" />
        <add key="section3key1" value="section3value1" />
        <add key="section3key2" value="section3value2" />
    </section3>
    <section2>
        <add key="section2key0" value="section2value0" />
        <add key="section2key1" value="section2value1" />
        <add key="section2key2" value="section2value2" />
    </section2>
    <section1>
        <add key="section1key0" value="section1value0" />
        <add key="section1key1" value="section1value1" />
        <add key="section1key2" value="section1value2" />
    </section1>
</configuration>

 

using System;
using System.Configuration;
using System.Windows.Forms;

namespace appString {
  public class appString {

    [STAThread]
    static void Main() {
      string[] sections = new string[] { "section1", "section2", "section3", "section4" };
      foreach (string section in sections) {
        for (int i = 0; i < 3; i++) {
          setString(section, section + "key" + i, section + "value" + i);
        }
      }
      string result = string.Empty;
      foreach (string section in sections) {
        result += string.Format("配置节点:{0}", section);
        for (int i = 0; i < 3; i++) {
          string childSection=string.Format("{0}",section + "key" + i);
          result += string.Format("\r\n\tkey:{0},", childSection);
          result += string.Format("value:{0}", getString(section, childSection));
        }
        result += "\r\n";
      }
      MessageBox.Show(result);
    }

    private static string getString(string section, string key) {
      try {
        Configuration configuration = config;
        AppSettingsSection newSection = appSection(section);
        if (newSection.Settings[key] == null) { return ""; }
        else { return newSection.Settings[key].Value; }
      }
      catch { return ""; }
    }

    private static void setString(string section, string key, string value) {
      try {
        Configuration configuration = config;
        AppSettingsSection appSetting = appSection(section);
        if (appSetting.Settings[key] == null) { appSetting.Settings.Add(key, value); }
        else { appSetting.Settings[key].Value = value; }
        configuration.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(section);
      }
      catch (System.Exception Ex) { }
    }

    public static string conncetString { set; get; }
    private static string _configFile = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\appString\user.config";
    private static string configFile { get { return _configFile; } }
    private static Configuration _config;

    public static Configuration config {
      get {
        if (_config != null) { return _config; }
        else {
          ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
          fileMap.ExeConfigFilename = configFile;
          _config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
          return _config;
        }
      }
    }

    public static AppSettingsSection appSection(string sectionName) {
      AppSettingsSection appSetting = (AppSettingsSection)config.GetSection(sectionName);
      if (appSetting != null) { return appSetting; }
      try {
        appSetting = new AppSettingsSection();
        config.Sections.Add(sectionName, appSetting);
        appSetting.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Modified);
        return appSetting;
      }
      catch (ConfigurationErrorsException Ex) { return null; }
    }
  }
}
View Code

 

转载于:https://www.cnblogs.com/mpy2003/p/6063354.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要在Winform应用程序中为树节点添加图标,可以使用ImageList和TreeView控件来实现。以下是一些步骤: 1. 在Visual Studio中,打开您的Winform应用程序。 2. 在工具箱中找到ImageList和TreeView控件,将它们都拖放到您的窗体上。 3. 在属性窗口中,为ImageList添加您要使用的图标。例如,您可以从资源文件中导入图标,或者使用系统自带的图标。 4. 在TreeView控件中,将ImageList属性设置为您刚才创建的ImageList。 5. 使用TreeView控件的节点事件来添加节点和图标。例如: ``` private void Form1_Load(object sender, EventArgs e) { // 添加一个根节点 TreeNode rootNode = new TreeNode("根节点"); // 添加节点,并为其设置图标 TreeNode childNode = new TreeNode("子节点"); childNode.ImageIndex = 0; // 设置图标在ImageList中的索引 childNode.SelectedImageIndex = 0; // 设置选中时的图标在ImageList中的索引 rootNode.Nodes.Add(childNode); // 将根节点添加到TreeView控件中 treeView1.Nodes.Add(rootNode); } ``` 在上面的示例中,我们为一个子节点添加了一个图标。您可以根据需要更改图标的索引,或者为其他节点添加不同的图标。 请注意,在使用树节点添加图标时,您需要事先准备好ImageList,并将其与TreeView控件关联。此外,您还可以在代码中使用Image.FromFile方法加载图标文件,并将其设置为节点的Image和SelectedImage属性,以便为每个节点添加不同的图标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值