Windows 服务配置文件 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/439334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Service Config File C#
提问by
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. 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.config 文件。安装后,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'. Am I doing something wrong here?
我不断看到的调试值是“1”而不是“3”。我在这里做错了吗?
回答by Alex Reitbort
App.config file should be renamed to your_exe_file_name.exe.config and placed near the exe file.
App.config 文件应重命名为 your_exe_file_name.exe.config 并放置在 exe 文件附近。
回答by Igor Zelaya
Maybe you are updating the wrong config file. You should double check that using
也许您正在更新错误的配置文件。你应该仔细检查使用
System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);
回答by EnocNRoll - AnandaGopal Pardue
Is it possible that you have more than one instance of the RuntimeFrequency entry defined? The ConfigurationManager reads the file from top to bottom and processes each setting individually. Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use.
是否可能定义了多个 RuntimeFrequency 条目实例?ConfigurationManager 从上到下读取文件并单独处理每个设置。因此,文件中定义的 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 中的条目时等待查看应用程序错误不存在。
回答by Dave Baghdanov
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.
也许您在该目录中有一个旧版本,即使您已经更新了项目中的配置文件,您的服务也在该目录中获取值。
回答by Aaron Fischer
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. Visual studio should update the final config file in your bin/debug folders.
当您处于调试模式时,请检查并查看 my_exe_file_name.vshost.exe.config 中的设置还确保您在 app.config 文件中进行了调整。Visual Studio 应该更新 bin/debug 文件夹中的最终配置文件。
回答by Aaron Fischer
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.
马丁。