gpt4 book ai didi

C# Windows 服务 - 从 ini 或 App.config 文件中读取

转载 作者:行者123 更新时间:2023-11-30 20:17:16 25 4
gpt4 key购买 nike

我有一个 Windows 轮询服务,每 10 分钟自动发送一封电子邮件。我使用 Thread.Sleep(new TimeSpan(0, 10, 0)); 让线程休眠 10 分钟,这现在是硬编码的。

为了避免硬编码,我尝试了 App.config 但没有成功。我想将硬编码移动到一些 .ini 文件中。如何从 C# Windows 服务读取 .ini 文件。

编辑:我尝试使用以下代码从我的 Windows 服务中读取。string pollingInterval = (string)new AppSettingsReader().GetValue("PollingInterval", typeof(string));给出以下错误。 配置系统初始化失败

最佳答案

app.config 是比 INI 文件更好的解决方案。

您的 app.config 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
.......
<appSettings>
<add key="TimerInterval" value="10" />
</appSettings>
.......
</configuration>

你读起来像:

int timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings["TimerInterval"]);

您需要导入命名空间 using System.Configuration; 并添加对 System.Configuration dll 的引用。

关于C# Windows 服务 - 从 ini 或 App.config 文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com