如何将我自己的 XML 配置文件添加到 Windows 服务 C#

How to Add My Own XML Configuration File to the Windows Service C#

我正在开发一个 Windows 服务,我已经在其中添加了一个特殊的配置 XML 文件。 使用 Visual Studio 进行调试时,服务可以读取该文件中的数据。 但是在构建文件时,该 XML 文件不会添加到构建文件夹中。 同样在安装服务后,它从 System32(默认位置)开始 出现错误 (Cannot find the file C:\Windows\System32\ServerAgent.xml) 文件未添加到安装文件夹。

如何将我的 XML 文件添加到构建文件夹和安装位置?

提前致谢。

  1. 在Visual Studio中点击文件树中的文件
  2. 右键单击并选择属性
  3. 有一个属性叫"Copy to Output Directory"
  4. Select "Copy Always" 或 "Copy if Newer"

最好在您的应用程序设置中指定 xml 文件的完整路径。请参阅此答案以查找您的应用程序所在的文件夹: