如何获取使用Topshelf创建的Windows服务的启动参数

时间:2016-05-25 11:20:30

标签: c# .net cmd windows-services topshelf

您好我使用topshelf dll创建了一个Windows服务。但是我无法获得启动参数。

我的代码: -

class Program
    {
        public static string filePath = "C:\\";
        public static string outputXml = "C:\\Pranay";
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 0 && !args[0].Equals("install", StringComparison.CurrentCultureIgnoreCase) && !args[0].Equals("uninstall", StringComparison.CurrentCultureIgnoreCase))
                {
                    //filePath = args[2];
                    //outputXml = args[3];
                }

                XmlConfigurator.ConfigureAndWatch(
                    new FileInfo(".\\log4net.config"));

                var host = HostFactory.New(x =>
                {
                    x.EnableDashboard();
                    x.Service<XmlConverterService>(s =>
                    {
                        s.SetServiceName("XmlConverterService");
                        s.ConstructUsing(name => new XmlConverterService());
                        s.WhenStarted(tc =>
                        {
                            XmlConfigurator.ConfigureAndWatch(
                                new FileInfo(".\\log4net.config"));
                            tc.Start(args);
                        });
                        s.WhenStopped(tc => tc.Stop());
                    });

                    x.RunAsLocalSystem();
                    x.SetDescription("I have created this to service");
                    x.SetDisplayName("PranayXMLService");
                    x.SetServiceName("PranayXMLService");
                });

                host.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }
        }

我已安装它,当我尝试将start参数传递为: -

sc start PranayXmlConverter C:\\ C:\\Pranay

什么都没有通过。要检查我在args中获得了什么,我已经用start(args)方法传递了它,当我打印args中的内容时,我知道输出如下:

-DisplayName
PranayXMLService
-servicename:PranayXMLService

我的问题是如何获得启动参数以及在哪里?

0 个答案:

没有答案