0

構成は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
   <sectionGroup name="CardValidator"
              type="Micro.CardValidator.Models.CardValidatorSectionGroup, Micro.CardValidator">
  <section name="Settings" type="Micro.CardValidator.Models.Settings, Micro.CardValidator" />
</sectionGroup>

 public class CardValidatorSectionGroup : ConfigurationSectionGroup {
    [ConfigurationProperty("Settings", IsRequired = false)]
    public Settings Settings {
        get { return (Settings) Sections["Settings"]; }
    }
}

public class Settings : ConfigurationSection {
    [ConfigurationProperty("LastSoftwareVersionInstalled", IsRequired = true)]
    public string LastSoftwareVersionInstalled {
        get { return (string) base["LastSoftwareVersionInstalled"]; }
        set { base["LastSoftwareVersionInstalled"] = value; }
    }

    [ConfigurationProperty("InstallationPlaceCode", IsRequired = true)]
    public int InstallationPlaceCode {
        get { return (int)base["InstallationPlaceCode"]; }
        set { base["InstallationPlaceCode"] = value; }
    }

    [ConfigurationProperty("InstallationPlaceName", IsRequired = true)]
    public string InstallationPlaceName {
        get { return (string)base["InstallationPlaceName"]; }
        set { base["InstallationPlaceName"] = value; }
    }
}

 public class Config {
    private Settings Settings { get; set; }

    public Station InstallationPlace {
        get {
            return new Station(Settings.InstallationPlaceCode, Settings.InstallationPlaceName);
        }
        set {
            Settings.InstallationPlaceCode = value.Id;
            Settings.InstallationPlaceName = value.ShortName;                
        }
    }

    public string LastInstalledSoftwareVersion {
        get { return Settings.LastSoftwareVersionInstalled; }
        set { Settings.LastSoftwareVersionInstalled = value; }
    }

    private readonly Configuration cfg;

    public Config() {
        cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        CardValidatorSectionGroup akmSectionGroup = (CardValidatorSectionGroup) cfg.SectionGroups["CardValidator"];

        Debug.Assert(akmSectionGroup != null, "AkmSectionGroup can not be null");
        Settings = akmSectionGroup.Settings;
    }

    public void Save(ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full) {
        cfg.Save(saveMode);
    }
}

保存後、構成ファイルにデータが表示されます。ただし、アプリケーションを閉じると、構成ファイルは空の状態に戻ります。

4

1 に答える 1