小编rad*_*ept的帖子

ConfigurationManager.RefreshSection("appSettings") 无效

背景:我使用计时器在 Windows 服务中定期执行一些工作。我希望计时器可以在运行时进行配置。我唯一能做的就是在启动时配置它。

我的解决方案:我使用 app.config 来配置计时器的开始时间和周期:

  <appSettings>
    <add key="StartTime" value="14:40:00"/>
    <add key="Period" value="24:00:00"/>
  </appSettings>
Run Code Online (Sandbox Code Playgroud)

我正在使用 FileSystemWatcher 来通知配置文件上的文件写入(将是 AppName.exe.config)

    public ConfigWatcher(params object[] args)
    {
        configurationChangedListeners = new List<INotifyConfigurationChanged>();

        string assemblyDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        NotifyFilters notifyFilters = NotifyFilters.LastWrite;
        _fileSystemWatcher = new FileSystemWatcher()
        {
            Path = assemblyDirectory,
            NotifyFilter = notifyFilters,
            Filter = "*.config"
        };
        _fileSystemWatcher.Changed += OnChanged;
        _fileSystemWatcher.EnableRaisingEvents = true;

        if (args != null)
        {
            foreach (var arg in args)
            {
                AddListener(arg);
            }
        }
    }

    private void OnChanged(object source, System.IO.FileSystemEventArgs e)
    {
        try
        { …
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
3836
查看次数

标签 统计

c# ×1