对,所以我有一个 Windows 服务,它每隔几分钟就会关闭并在服务器上做一些工作。该服务从 App.config 读取有关它连接到的服务器的大量信息(主机、用户名、端口等),并且效果很好。
我现在有一个要求,该服务可以满足 n 个不同的服务器。所以现在我的服务需要从 App.config 中读取,并按顺序为 server1..serverN 做它需要做的事情。等待预定的时间,然后再次从 server1 开始。
我不知道如何或什么是在 App.config 中存储 n 组服务器设置的最佳方式,然后以编程方式确定有多少组设置,然后读取每组设置。
我想过有一个设置告诉我有 5 台服务器,然后设置 server1..server5 的设置,但这真的不优雅。
有一个更好的方法吗?
我的完整源文件如下:
using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;
namespace FTPConfig
{
public class MyAppConfig : ConfigurationSection
{
public static MyAppConfig GetConfiguration()
{
MyAppConfig configuration = ConfigurationManager.GetSection("MainSettings") as MyAppConfig;
if (configuration != null) return configuration;
return new MyAppConfig();
}
}
[ConfigurationProperty("host", IsRequired = true)]
public String Host
{
get
{
return this["host"] as string;
}
}
}