.net Windows 服务在哪个目录中运行?

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

What directory does a Windows Service run in?

.netwindows-services

提问by Guy

I've created a very simple .NET Windows Service and installed it using InstallUtil.exe utility.

我创建了一个非常简单的 .NET Windows 服务并使用 InstallUtil.exe 实用程序安装它。

In the service I have a piece of code as such:

在服务中,我有一段这样的代码:

if (File.Exists("test_file.txt"))
{
   // Do something clever
}

I've created a file called test_file.txt in the same directory as the service but the commented part of the code is never being executed...?

我在与服务相同的目录中创建了一个名为 test_file.txt 的文件,但代码的注释部分从未被执行......?

采纳答案by Jeff

Services are started from an application called Service Control Manager. This application lives in the system directory %WinDir%\System32

服务从称为服务控制管理器的应用程序启动。此应用程序位于系统目录%WinDir%\System32 中

On a Windows 7 Ultimate - 64 bits this path is actually : %WinDir%\SysWOW64

在 Windows 7 Ultimate - 64 位上,此路径实际上是:%WinDir%\SysWOW64

For more information see Service Control Manager at MSDN.

有关详细信息,请参阅MSDN 上的服务控制管理器

Thanks Harper Shelby for pointing out problem with orginal post.

感谢 Harper Shelby 指出原始帖子的问题。

回答by Dirk Vollmar

System.Diagnostics.Trace.WriteLine(Directory.GetCurrentDirectory());

will output the current directory. Put that code in the startup method of your service and use a tool like DebugViewto check the output. Then you will know the startup folder of your service.

将输出当前目录。将该代码放在您的服务的启动方法中,并使用DebugView 之类的工具来检查输出。然后你就会知道你的服务的启动文件夹。

This simple technique will be useful with many problems in service development, especially to debug service startup.

这种简单的技术对于解决服务开发中的许多问题非常有用,尤其是调试服务启动时。

You probably expected the working folder of your service to be the folder where the service executable is in (so did I). You can change to that folder using the following lines of code:

您可能希望服务的工作文件夹是服务可执行文件所在的文件夹(我也是如此)。您可以使用以下代码行更改到该文件夹​​:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

回答by yantaq

You can make it work like so:

你可以让它像这样工作:

string cwd = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
Directory.SetCurrentDirectory(cwd ?? ".");

回答by Riga

Wanted also to know in which folder a Windows service was running but source code was not mine so could not modify it. Typing in Command Prompt sc qc <service name>displays the folder in BINARY_PATH_NAME.

还想知道 Windows 服务在哪个文件夹中运行,但源代码不是我的,因此无法修改。在命令提示符中键入sc qc <service name>会显示 BINARY_PATH_NAME 中的文件夹。

C:\>sc qc
DESCRIPTION:
        Queries the configuration information for a service.
USAGE:
        sc <server> qc [service name] <bufferSize>

when query MyService get:

当查询 MyService 时得到:

C:>sc qc MyService

C:>sc qc MyService

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: MyService
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 3   DEMAND_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "D:\Routines\MyService\MyService.exe"
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : MyService
        DEPENDENCIES       :
        SERVICE_START_NAME : LocalSystem