如何使用 C# 找到 Windows 服务的安装目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/491735/
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
How do I find the install directory of a Windows Service, using C#?
提问by endian
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.
我很确定 Windows 服务在使用 InstallUtil.exe 安装时将 C:\winnt(或类似的)作为其工作目录。有什么方法可以访问或以其他方式捕获(在安装时)最初安装服务的目录?目前我正在将它手动输入到 app.exe.config 文件中,但这是非常手动的,感觉就像一个黑客。
Is there a programmatic way, either at run time or install time, to determine where the service was installed from?
是否有一种编程方式(在运行时或安装时)来确定服务的安装位置?
采纳答案by Steve Wranovsky
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);
回答by Jon Skeet
Do you mean you want the directory containing the assembly? If so, that's easy: use Assembly.Location
.
你的意思是你想要包含程序集的目录?如果是这样,那很简单:使用Assembly.Location
.
I wouldn't try to changethe working directory of the process though - I wouldn't be surprised if that had nasty side effects, if indeed you're allowed to do it.
不过,我不会尝试更改进程的工作目录 - 如果确实允许您这样做会产生令人讨厌的副作用,我不会感到惊讶。
回答by Quassnoi
InstallUtil.exe
calls ServiceInstaller.Install()
of your application at install time.
InstallUtil.exe
ServiceInstaller.Install()
在安装时调用您的应用程序。
Override it, add it to the list of your project's Installers
and get any information you need.
覆盖它,将其添加到您的项目列表中Installers
并获取您需要的任何信息。
回答by Ramon Smits
I did not know the Directory.SetCurrentDirectory
method. Usually I do:
我不知道Directory.SetCurrentDirectory
方法。通常我这样做:
Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
回答by DiligentKarma
Though very late, but it may help somebody. I solved this issue by using AppDomain.CurrentDomain.BaseDirectory
虽然很晚,但它可能会帮助某人。我通过使用 AppDomain.CurrentDomain.BaseDirectory 解决了这个问题
string someFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\SomeResource.xml";
AppDomain.CurrentDomain.BaseDirectory gave the directory where the windows service was actually isntalled, not the C:\Windows\system32\ path.
AppDomain.CurrentDomain.BaseDirectory 给出了实际安装 windows 服务的目录,而不是 C:\Windows\system32\ 路径。
I saw it later that @Ramon has already posted the same solution.
后来我看到@Ramon 已经发布了相同的解决方案。