C# Windows 服务 app.config 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14074563/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:36:13  来源:igfitidea点击:

Windows service app.config location

c#windows-server-2008

提问by Mité

I have installed a C# Windows Service on Windows Server 2008. I installed it with InstallUtil. The service reads some data from the app.config file and it is doing it fine. Can you tell me where this file is located after installing the service? I have been looking for hours but can't find it.

我在 Windows Server 2008 上安装了 C# Windows 服务。我用 InstallUtil 安装了它。该服务从 app.config 文件中读取一些数据,并且运行良好。你能告诉我安装服务后这个文件的位置吗?我已经找了几个小时,但找不到。

采纳答案by Umar Farooq Khawaja

You can verify the exact location of the installed Windows Service by following the steps below:

您可以按照以下步骤验证安装的 Windows 服务的确切位置:

  1. Bring up the list of Windows Services by clicking the "Services" icon under the "Administrative Tools" icon. You can also get this list by typing "View local services" in the Search Menu under the Start Menu.

  2. Select your Windows service in the list of installed services, right-click and then select Properties. You can also double click on row representing the service.

  3. Locate the "Path to executable" value on the Properties dialog box. The value will include any command line parameters.

  4. Open the folder in which the service executable resides.

  1. 单击“管理工具”图标下的“服务”图标,调出 Windows 服务列表。您还可以通过在“开始”菜单下的“搜索”菜单中键入“查看本地服务”来获取此列表。

  2. 在已安装服务列表中选择您的 Windows 服务,右键单击然后选择属性。您还可以双击代表服务的行。

  3. 在“属性”对话框中找到“可执行文件的路径”值。该值将包括任何命令行参数。

  4. 打开服务可执行文件所在的文件夹。

If the Windows service has been built with .NET Framework, its configuration will be stored in the corresponding .config file, i.e., the name of the executable suffixed by ".config", e.g., if the name of the executable is "XyzService.exe", then the name of the .config file will be "XyzService.exe.config".

如果Windows 服务是用.NET Framework 构建的,其配置将存储在相应的.config 文件中,即以“.config”为后缀的可执行文件的名称,例如,如果可执行文件的名称是“XyzService.config”。 exe”,那么 .config 文件的名称将是“XyzService.exe.config”。

A couple of things to note:

有几点需要注意:

  • If you installed the service after building it on the same machine using say, Visual Studio, then Visual Studio would have transformed the App.config file from the project and placed it in the build output folder automatically (and renamed it appropriately using the above naming convention).

  • If your machine is set to hide file extensions in Windows Explorer, you will see 2 files "XyzService" and "XyzService.exe". In this case, the "XyzService.exe" is your config file. If you then switch off the option to hide file extenions in Windows Explorer, you will then begin to see "XyzService.exe" and "XyzService.exe.config".

  • If you cannot find a corresponding .exe.config file, then it is possible that the code within the service is falling back to default values. In this case, you can place a properly named and formatted config file alongside the service executable and then restart the service and everything should be fine.

  • 如果您在使用 Visual Studio 在同一台机器上构建服务后安装了该服务,那么 Visual Studio 将转换项目中的 App.config 文件并将其自动放置在构建输出文件夹中(并使用上述命名对其进行适当重命名习俗)。

  • 如果您的机器设置为在 Windows 资源管理器中隐藏文件扩展名,您将看到 2 个文件“XyzService”和“XyzService.exe”。在这种情况下,“XyzService.exe”是您的配置文件。如果您随后关闭了在 Windows 资源管理器中隐藏文件扩展名的选项,您将开始看到“XyzService.exe”和“XyzService.exe.config”。

  • 如果找不到相应的 .exe.config 文件,则服务中的代码可能会回退到默认值。在这种情况下,您可以在服务可执行文件旁边放置一个正确命名和格式化的配置文件,然后重新启动服务,一切都应该没问题。

回答by Kishore Kumar

The same place where your application (Windows Service) is.

与您的应用程序(Windows 服务)所在的位置相同。

Check it out, if it's not there place it in the same directory as of service.

检查一下,如果它不在那里,请将其放在与服务相同的目录中。

回答by Tilak

It is the same location from where you have registered service using installutiltool.

它与您使用installutil工具注册服务的位置相同。

回答by Brandon

The App.config is likely called {ProjectName}.exe.configgiven the fact that it is a Windows Service. Check to see if that file exists and is what you are looking for.

{ProjectName}.exe.config考虑到App.config 是Windows 服务,它可能会被调用。检查该文件是否存在并且是您要查找的文件。

回答by Michaelb

If you have a live environment (and from your question it seems like you do), you can check what's actually happening using the superior Process Monitor utility. But usually the .config fileis located right next to the .exe, and named the same.

如果您有一个实时环境(并且从您的问题看来您确实如此),您可以使用卓越的 Process Monitor 实用程序检查实际发生的情况。但通常 .config 文件位于 .exe 旁边,并命名相同。

回答by sgmoore

According to Microsoft

根据微软

For client executables, the application configuration file resides in the same directory as the application's executable and has the same base name as the executable with a .config extension.

对于客户端可执行文件,应用程序配置文件与应用程序的可执行文件位于同一目录中,并且与可执行文件具有相同的基本名称,但带有 .config 扩展名。

Note, if your exe is called appname.exe, and you have Windows explorer set to hide extensions, then your application will display as appname and your config file then it will be displayed as appname.exe (even though the true name is appname.exe.config)

请注意,如果您的 exe 名为 appname.exe,并且您将 Windows 资源管理器设置为隐藏扩展名,那么您的应用程序将显示为 appname,而您的配置文件则将显示为 appname.exe(即使真实名称是 appname.exe)。 exe.config)

As others have pointed out, InstallUtil doesn't do anything with the config file and it should have copied to the server in the same manner as the exe itself.

正如其他人指出的那样,InstallUtil 不会对配置文件做任何事情,它应该以与 exe 本身相同的方式复制到服务器。