C# 我如何设置“排序”;从“开始”-Windows服务的路径
我有以下类,Windows Installer项目使用该类来安装服务:C# 我如何设置“排序”;从“开始”-Windows服务的路径,c#,windows-services,C#,Windows Services,我有以下类,Windows Installer项目使用该类来安装服务: [RunInstaller(true)] public sealed class Installer : System.Configuration.Install.Installer { private readonly string _installDir; public Installer() { var locatedAssembly = this.GetType().Asse
[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);
}
}
如果我们在控制台应用程序内调用以下代码,则将获取程序集的目录:
using (var stream = File.Open("foo.store", FileMode.OpenOrCreate))
如果我从Windows服务运行该行,将改为使用C:\Windows\System32\
我怎样才能改变这种行为
澄清一下:我不想利用任何程序集监视(从
this.GetType()
..获取程序集的路径)或appsettings中的任何内容。我希望它在调用方不使用任何魔法的情况下直接工作:)您需要从配置文件或注册表中读取文件夹位置。没有类似的起始目录。不要信任当前目录。如果文件位于服务用途之外:
string sdir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
恢复可执行文件所在的路径,并将其用作查找文件的基本路径。我知道,我不应该信任当前目录-如果不在非常封闭的组件中使用此目录,我不会这样做-不公开,我将100%控制整个单元…:)好。。。该死的。。。没有机会:(不…没有注册表黑客-这将是残酷的:)配置黑客将是好的,如果我想有灵活性。。。但是整个单位都是内部的,没有劫持。。。但是谢谢你的信息!你很乐意编写一个服务,但你对从注册表中读取单个值神圣吗?对从注册表中读取单个值神圣吗?事实上:是的,我不喜欢将注册表用于这个简单任务的想法。。。一定有更简单的东西!注册表是服务的默认配置选项。每个服务都有一个专用的密钥。嗯,最初来自控制台/web应用程序,我既不习惯也不想利用注册表来完成这个简单的任务。但我真的很高兴你的见解-谢谢!