【问题标题】:Where is the user.config of a .NET service located?.NET 服务的 user.config 位于何处?
【发布时间】:2014-05-01 09:59:06
【问题描述】:

我正在使用 installutil.exe. (.NET 4.0) 安装基于 .NET 的 Windows 服务,该服务包括 ServiceName.exe.config file。此文件与 .exe 文件本身存储在同一目录中。但似乎这个文件只保存默认值(如“标准”.NET 应用程序中)。在服务运行时更改设置不会在位于安装路径中的 ServiceName.exe.config 中生效(作为对 http://stackoverflow.com/questions/17154936/net-service-config-file-locationother 的回答类似的问题建议)。 查看C:\Windows\Users\"Username"\AppData\Local\ 也不会成功。

更改设置正在工作,我通过将一些设置值写入日志文件来检查。唯一的问题是,值存储在哪个文件和哪个位置?

编辑:

配置文件如下所示。

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="MeLoQDASExportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="MeLoQDASExportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
    </startup>
    <applicationSettings>
        ...
    </applicationSettings>
    <userSettings>
        <MeLoQDASExportService.Properties.Settings>
            <setting name="logName" serializeAs="String">
                <value>ExportLog.txt</value>
            </setting>
            <setting name="lastExport" serializeAs="String">
                <value>1990-01-01</value>
            </setting>
        </MeLoQDASExportService.Properties.Settings>
    </userSettings>
</configuration> 

这是已安装服务文件夹中的文件。它仍然包含初始值,但从服务输出到文本文件的值会显示实际值(例如,“lastExport”的当天日期)

【问题讨论】:

  • 没有 c:\windows\users 目录,因此您无法查看正确的目录。您没有提及您用于服务的用户帐户,系统帐户的配置文件存储在 win8 上的 c:\windows\system32\config\systemprofile 中。在 superuser.com 上询问有关它的更多问题
  • 我接触过的 Windows 服务使用了与安装服务(安装路径)相同的文件夹中的 .exe.config。您是否尝试更新 或 下的值?
  • 请看看我在上面帖子中的编辑
  • 有几个可能的位置。见:stackoverflow.com/questions/6379020/…

标签: c# .net service settings


【解决方案1】:

试试 System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel 级别) http://msdn2.microsoft.com/en-us/library/ms134265.aspx

传入 ConfigurationUserLevel.PerUserRoamingAndLocal(或者也尝试 PerUserRoaming)。然后在返回的 Configuration 对象上,获取 FilePath 属性。

【讨论】:

    【解决方案2】:

    我刚刚发现使用Process Monitor

    c:\Windows\SysWOW64\config\systemprofile\AppData\

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 2016-10-06
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2014-11-04
      相关资源
      最近更新 更多