[英]How to use the value of appSettings from App.Config file when creating a Windows Service
我正在嘗試創建一個 Windows 服務器。 我在 C# 中有一些邏輯
string urlToPing = ConfigurationSettings.AppSettings["UrlToPing"].ToString();
Stream data = client.OpenRead(urlToPing);
我需要閱讀
這是我的 App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="UrlToPing" value="http://mysite.com"/>
</appSettings>
</configuration>
我是Windows服務的新人,我的問題:
我的第二個問題是我找到了一個解決方案
將System.Configuration的引用添加到您的代碼文件中。
using System.Configuration;
現在可以正確引用該設置......
ConfigurationManager.AppSettings["UrlToPing"].ToString();
對於第一個問題,當您構建可執行項目(Windows服務,控制台應用程序等)時,它會將app.config重命名為“YourApplication”.exe.config,其中“YourApplication”是啟動程序集的名稱。 然后它會將文件復制到輸出文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.