如何找到一个Windows服务的安装目录,使用C#? [英] How do I find the install directory of a Windows Service, using C#?

查看:157
本文介绍了如何找到一个Windows服务的安装目录,使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,一个Windows服务获取C:使用InstallUtil.exe安装的时候\winnt(或类似)作为其工作目录。有什么办法我可以访问,或以其他方式获取(在安装时),从最初的安装目录中的服务?目前,我手动输入该到app.exe.config文件,但是这是可怕的手动,感觉就像一个黑客。

I'm pretty sure that a Windows service gets C:\winnt (or similar) as its working directory when installed using InstallUtil.exe. Is there any way I can access, or otherwise capture (at install time), the directory from which the service was originally installed? At the moment I'm manually entering that into the app.exe.config file, but that's horribly manual and feels like a hack.

有没有一种编程方法,无论是在运行时或安装时间,来确定该服务是从安装?

Is there a programmatic way, either at run time or install time, to determine where the service was installed from?

推荐答案

您可以使用反射来获取的位置执行的程序集。这里有一个简单的常规的设置工作目录执行的程序集使用反射的位置:

You can use reflection to get the location of the executing assembly. Here's a simple routine that sets the working directory to the location of the executing assembly using reflection:

String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = System.IO.Path.GetDirectoryName(path);
Directory.SetCurrentDirectory(path);

这篇关于如何找到一个Windows服务的安装目录,使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆