21

I tried to pass parameters to a windows service.

Here is my code snippet:

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();
        } 
    }

How do I pass UserName and Password to this windows service?

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Ragaei Mahmoud
  • 447
  • 2
  • 11
  • 26

6 Answers6

33

You can pass parameters on startup like this:

  1. Right click on MyComputer and select Manage -> Services and Applications -> Services
  2. Right click on your service, select Properties and you should then see the Start Parameters box under the General tab.

If you enter there for example User Password you will get these parameters in protected override void OnStart(string[] args) as args. then use it like this:

protected override void OnStart(string[] args)
{
    base.OnStart(args);
    UserName = args[0];
    Password = args[1];
    //do everything else
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • 8
    as of win7 (at least) this only works to set the parameters to use when starting the service from the Properties dialog box – Martin Serrano Nov 20 '13 at 21:05
  • @MartinSerrano Hi. Sorry didn't understood what you mean – Renatas M. Nov 21 '13 at 07:33
  • the dialog only lets you set those parameters for starting the service once. to make it permanent you have to do something like in the 2nd answer here: http://stackoverflow.com/questions/2243753/how-to-make-a-windows-service-with-parameters – Martin Serrano Nov 24 '13 at 00:42
5

You are going to have to load these values up from an external source. The easiest is to load them directly from an app.config file, using the Configuration Manager. Something like this: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Robert Anton Reese
  • 535
  • 1
  • 7
  • 18
3

You can use configuration file, Registry or any type of databases.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
1

Use Environment.GetCommandLineArgs()

Julian
  • 1,050
  • 1
  • 12
  • 23
1

You can pass parameters to windows service when starting it. Run cmd with administrator privileges and enter following command:

sc start <ServiceName> param1 param2

And if you want to use it one time forever you can store parameters into a file or app.config.

0

The two cleanest ways to pass arguments (without using registry, files or a database) to a Windows Service at runtime is using Named Pipes or setting up a WCF Service in windows, that your client calls into. By default, a Windows Service is meant to be a repetitive process that runs.

If you use WCF, turn it on in Add Remove Programs (or Programs and Features for Windows 7).

Named Pipes:

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

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245