C# 如何使用命令行 + 参数注册 .Net 服务?

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

How to register a .Net service with a command line + parameters?

c#.net-3.5service

提问by Patrick Desjardins

I would like to be able to pass parameters in the installationof the service. I have modified the C# code of the class that inherit from Installer... My problem is the InstallUtil.exe doesn't work with parameters (well not as I know).

我希望能够在服务的安装中传递参数。我修改了从 Installer 继承的类的 C# 代码......我的问题是 InstallUtil.exe 不能使用参数(据我所知)。

Any suggestion?

有什么建议吗?

采纳答案by Vasu Balakrishnan

We have the same scenario and it works. You've to pass the parameters as follows

我们有相同的场景并且它有效。您必须按如下方式传递参数

InstallUtil.exe /Param1="Value" /Param2="Value" "Path to your exe"

Then you've to override Install method on your installer

然后你必须覆盖安装程序上的 Install 方法

public override void Install(System.Collections.IDictionary stateSaver)
{
     var lParam1 =   GetParam("Param1");
}

private string GetParam(string pKey)
{
        try
        {
            if (this.Context != null)
            {
                if (this.Context.Parameters != null)
                {
                    string lParamValue = this.Context.Parameters[pKey];
                    if (lParamValue != null)
                        return lParamValue;
                }
            }
        }
        catch (Exception)
        {
        }
        return string.Empty;
    }

回答by Faqa

Try NSIS. It's a scripting language built for installations, allows you to do all manner of complex things. Personally, I'd use it for any installation that goes beyond "Next, Next, Next". Very slick, and not all that hard to learn.

试试NSIS。它是一种为安装而构建的脚本语言,允许您执行各种复杂的操作。就个人而言,我会将它用于任何超出“下一步、下一步、下一步”的安装。非常圆滑,而且并不难学。

回答by JP Alioto

Build and installer with either Visual Studio or something like Wix. In Visual Studio, you can do a Custom Actionand pass in parameters in the CustomActionData field.

使用 Visual Studio 或Wix 之类的工具构建和安装程序。在 Visual Studio 中,您可以执行自定义操作并在 CustomActionData 字段中传递参数。

回答by Mike Marshall

Actually it can be done with InstallUtil.exe, the .NET installer utility that comes with the .NET Framework.

实际上,它可以通过 InstallUtil.exe 来完成,它是 .NET Framework 附带的 .NET 安装程序实用程序。

Take a look at this CodeProjectarticle.

看看这篇CodeProject文章。