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

标签 c# windows-services ini

我有一个 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/

相关文章:

java - ini4j 错误地解析节名称为 [mm/yyyy] 的 .ini 文件?

c# - SqlCommand 参数与 String.Format

c# - 将值从 Grid-View 内部的 Modal 弹出窗口 Extender 传递到父表单的文本框

java - 远程调试作为 Windows 服务启动的 Java 应用程序

c# - 使用 ServiceController 类与 Windows 服务通信

python - 如何用 Python3 读写 INI 文件?

c# - 如何为标签做背景没有颜色?

c# - 如何为我的 DLL 创建 XML Intellisense 文件?

iis - Windows 7中IIS的inetmgr.exe的默认位置是什么,因为我的Windows搜索均无法正常进行。有其他选择吗?

c# - 用于存储的 XML 或 INI 文件?