如何向Windows Service传递参数?

问题描述 投票:0回答:7

我尝试将参数传递给 Windows 服务。

这是我的代码片段:

class Program : ServiceBase
{
    public String UserName { get; set; }
    public String Password { get; set; }

    static void Main(string[] args)
    {
        ServiceBase.Run(new Program());
    }

    public Program()
    {
        this.ServiceName = "Create Users Service";
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        String User = UserName;
        String Pass = Password;
        try
        {
            DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");

            DirectoryEntry NewUser = AD.Children.Add(User, "user");
            NewUser.Invoke("SetPassword", new object[] { Pass });
            NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
            NewUser.CommitChanges();
            DirectoryEntry grp;
            grp = AD.Children.Find("Administrators", "group");
            if (grp != null)
            {
                grp.Invoke("Add", new object[] { NewUser.Path.ToString() });
            }
            Console.WriteLine("Account Created Successfully");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        } 
    }

如何将用户名和密码传递给此 Windows 服务?

c# windows-services
7个回答
33
投票

您可以在启动时传递参数,如下所示:

  1. 右键单击我的电脑并选择管理 -> 服务和应用程序 -> 服务
  2. 右键单击您的服务,选择“属性”,然后您应该会在“常规”选项卡下看到“启动参数”框。

如果您在那里输入,例如

User Password
,您将在
protected override void OnStart(string[] args)
中获得这些参数作为 args。 然后像这样使用它:

protected override void OnStart(string[] args)
{
    base.OnStart(args);
    UserName = args[0];
    Password = args[1];
    //do everything else
}

5
投票

您将必须从外部源加载这些值。最简单的方法是使用配置管理器直接从 app.config 文件加载它们。像这样的东西:http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx


3
投票

您可以使用配置文件注册表或任何类型的数据库


1
投票

使用Environment.GetCommandLineArgs()


1
投票

Windows服务启动时可以传递参数。 以管理员权限运行cmd并输入以下命令:

sc start <ServiceName> param1 param2

如果您想永远使用一次,您可以将参数存储到文件或 app.config 中。


0
投票

在运行时将参数(不使用注册表、文件或数据库)传递给 Windows 服务的两种最简洁的方法是使用命名管道或在 Windows 中设置客户端调用的 WCF 服务。默认情况下,Windows 服务是一个运行的重复进程。

如果您使用 WCF,请在“添加删除程序”(或 Windows 7 的“程序和功能”)中将其打开。

命名管道:

https://msdn.microsoft.com/en-us/library/aa365590(v=vs.85).aspx


0
投票

除了我在程序中安装该服务之外,我也遇到了同样的问题。我正在使用 sc.exe 安装服务,秘密是将应用程序路径及其命令行参数传递给双引号内的 sc.exe:

从命令行:

sc create myservice "myservice.exe -fappsettings.json"  

代码中:

using (var process = new Process())
{
  process.StartInfo.FileName = "sc.exe";
  process.StartInfo.Arguments = $"create {serviceName} start= auto error= normal binpath= \"{Environment.ProcessPath} -fappsettings.ini\" displayname= {displayName}";
  process.StartInfo.Verb = "runas";
  process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  process.Start();
  process.WaitForExit();
}

对于 OP,在服务上下文中的命令行上明确传递用户名和密码存在安全风险。正如其他人提到的那样,将它们放入设置文件中,并使用上面的内容来指定设置。请记住,服务控制管理器会将当前工作目录设置为 c:\windows\services32 或类似目录。

A

© www.soinside.com 2019 - 2024. All rights reserved.