如何为 Windows 服务设置某种“开始"路径 [英] How can I set sort of "Start in"-path for a Windows Service

查看:20
本文介绍了如何为 Windows 服务设置某种“开始"路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,由 Windows Installer 项目使用,用于安装服务:

I have following class, which is used by a Windows Installer project, to install a service:

[RunInstaller(true)]
public sealed class Installer : System.Configuration.Install.Installer
{
    private readonly string _installDir;

    public Installer()
    {
        var locatedAssembly = this.GetType().Assembly.Location;
        this._installDir = Path.GetDirectoryName(locatedAssembly);
        var serviceProcessInstaller = new ServiceProcessInstaller
        {
            Account = ServiceAccount.LocalSystem
        };

        var serviceInstaller = new ServiceInstaller
        {
            ServiceName = Settings.Service.Name,
            StartType = ServiceStartMode.Automatic
        };

        this.Installers.Add(serviceProcessInstaller);
        this.Installers.Add(serviceInstaller);
        this.Context = new InstallContext(this._installDir + @"\install.log", new[]
        {
            string.Format("/assemlypath={0}", locatedAssembly)
        });
    }

    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        var serviceController = new ServiceController(Settings.Service.Name);
        serviceController.Start();
        serviceController.WaitForStatus(ServiceControllerStatus.Running);
    }
}

如果我们在控制台应用程序中调用以下代码,程序集的目录将被占用:

If we call the following code inside a console application, the directory of the assembly will be taken:

using (var stream = File.Open("foo.store", FileMode.OpenOrCreate))

如果我从 Windows 服务运行该行,则将采用 C:\Windows\System32\.

If I run the line from my Windows Service, C:\Windows\System32\ will be taken instead.

我该如何改变这种行为?

How can I change this behaviour?

澄清:我不想使用任何程序集监视(从 this.GetType()... 获取程序集的路径)或 appsettings 中的任何内容.我希望它在调用方没有任何魔法的情况下直接工作:)

For clarification: I do not want to utilize any assembly-spying (get the path of the assembly from this.GetType()...) or anything in the appsettings. I want it to work straight without any magic on the caller side :)

推荐答案

您需要从配置文件或注册表中读取文件夹位置.没有类似的起始目录.

You will need to read the folder location from a configuration file, or the registry. There's no analogue of starting directory.

这篇关于如何为 Windows 服务设置某种“开始"路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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