如何将参数传递给Windows服务? [英] How to pass parameters to Windows Service?
本文介绍了如何将参数传递给Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将参数传递到Windows服务
I tried to pass parameters to a windows service.
下面是我的代码片段:
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?
推荐答案
您可以传递启动参数是这样的:
You can pass parameters on startup like this:
- 右键单击我的电脑,选择管理 - >服务和应用程序 - >服务
- 右键点击您服务,选择属性,然后你应该看到启动参数框中在常规选项卡。
如果您输入有例如用户密码
你整体将会达成目标参数保护覆盖无效的OnStart(字串[] args)
为ARGS。
然后用它是这样的:
If you enter there for example User Password
you will get there 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
}
这篇关于如何将参数传递给Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文