简体   繁体   English

Windows服务配置文件C#

[英]Windows Service Config File C#

I've developed a windows service application using Visual Studio 2008 / C#. 我使用Visual Studio 2008 / C#开发了一个Windows服务应用程序。

I have an app.config file in the project. 我在项目中有一个app.config文件。 When installed, the app.exe.config file appears beside the executable but it appears not to be reading the values from it when I try to access them through ConfigurationManager.AppSettings. 安装后,app.exe.config文件出现在可执行文件旁边,但当我尝试通过ConfigurationManager.AppSettings访问它们时,它似乎没有从中读取值。

Has it copied the config file elsewhere or is there some other problem I don't know about? 它是否已将配置文件复制到其他位置,还是存在其他一些我不知道的问题?

Thanks in advance, 提前致谢,

Martin. 马丁。

Edit: The config file name is infact my_exe_file_name.exe.config, it looks like: 编辑:配置文件名实际上是my_exe_file_name.exe.config,它看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

    <add key="RuntimeFrequency" value="3" />

  </appSettings>
</configuration>

and I am trying to read via: 而我正在尝试阅读:

ConfigurationManager.AppSettings["RuntimeFrequency"]

The debug value I continually see is '1' and not '3'. 我不断看到的调试值是'1'而不是'3'。 Am I doing something wrong here? 我在这里做错了吗?

I located the error and it was related to file permissions. 我找到了错误,它与文件权限有关。 After installing the service, my local user account didn't have access to modify the app.exe.config file. 安装该服务后,我的本地用户帐户无权修改app.exe.config文件。

The tool I was using to edit was not informing me it was being denied access to save the file - that's notepad++ if anyone is interested - so I couldn't see that it wasn't saving over the old config file. 我用来编辑的工具没有告诉我它被拒绝保存文件的权限 - 如果有人有兴趣那就是记事本++ - 所以我看不出它没有保存在旧的配置文件中。

Solved now, thanks everyone. 现在解决了,谢谢大家。

Martin. 马丁。

When you are in debug mode check and see what settings are in the my_exe_file_name.vshost.exe.config Also make sure you adjust this in the app.config file. 当您处于调试模式时,请检查并查看my_exe_file_name.vshost.exe.config中的设置。还要确保在app.config文件中进行调整。 Visual studio should update the final config file in your bin/debug folders. Visual Studio应更新bin / debug文件夹中的最终配置文件。

Maybe you are updating the wrong config file. 也许您正在更新错误的配置文件。 You should double check that using 你应该仔细检查一下

System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);

Generally for the Windows Services that I write, i drop the appName.exe.config file into C:\\WINDOWS\\system32\\ 通常,对于我编写的Windows服务,我将appName.exe.config文件放入C:\\ WINDOWS \\ system32 \\

Perhaps you have an older version in that directory, which is where your service is getting the value, even though you've updated the config file in your project. 也许你在该目录中有一个旧版本,即使你已经更新了项目中的配置文件,这也是你的服务获得价值的地方。

应将App.config文件重命名为your_exe_file_name.exe.config并放在exe文件附近。

Is it possible that you have more than one instance of the RuntimeFrequency entry defined? 您是否可能定义了多个RuntimeFrequency条目实例? The ConfigurationManager reads the file from top to bottom and processes each setting individually. ConfigurationManager从上到下读取文件并单独处理每个设置。 Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use. 因此,文件中定义的RuntimeFrequency的最后一个值是它将使用的值。

If you want to know for sure if your file is being used, I would simply remove or comment out any definition for RuntimeFrequency (copy/paste errors do happen) and wait to see an application error when ConfigurationManager attempts to reference an entry in AppSettings that does not exist. 如果您想确定您的文件是否正在使用,我只需删除或注释掉RuntimeFrequency的任何定义(发生复制/粘贴错误),并在ConfigurationManager尝试引用AppSettings中的条目时等待查看应用程序错误不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM