如何使用UserInteractive = false从bat文件运行exe

时间:2022-02-16 16:02:28

I created console app, which, if run as exe, uses Console to interact with user (for debug purposes) and, if run from bat-file must install a topshelf service. But now i have a problem, because when execute such a bat file:

我创建了控制台应用程序,如果以exe身份运行,则使用Console与用户交互(出于调试目的),如果从bat-file运行,则必须安装topshelf服务。但是现在我有一个问题,因为当执行这样的bat文件时:

cd "%~dp0"
TCPAgent.exe install

UserInteractive is true (because bat file creates a console window) and my programm appears. How can i get topshelf service installed? I don't want to create a bat file to run file, because for me it looks like an overkill...

UserInteractive为true(因为bat文件创建了一个控制台窗口),我的程序出现了。如何安装topshelf服务?我不想创建一个bat文件来运行文件,因为对我来说它看起来像一个矫枉过正的...

Here is code of y Programm.cs:

这是y Programm.cs的代码:

static void Main(string[] args)
    {
        if (Environment.UserInteractive)
        {
            var agent = new TcpAgent();
            agent.Start();
            Console.ReadLine();
            agent.Stop();

        }
        else
        {
            HostFactory.Run(
            x =>
            {
                x.Service<TCPAgentServiceControl>(
                    s =>
                    {
                        s.ConstructUsing(name => new TCPAgentServiceControl());
                        s.WhenStarted((control, hostControl) => control.Start(hostControl));
                        s.WhenStopped((control, hostControl) => control.Stop(hostControl));
                    });
                x.UseNLog(LogManager.GetCurrentClassLogger().Factory);
                x.RunAsLocalSystem();
            });
        }
    }

I'll be gratefull for any help!

我会感激任何帮助!

1 个解决方案

#1


0  

I believe you should pass your value as a command-line argument in your bat file. When you run it in console the project will not get the comment-line argument, therefore it will be in debug-mode

我相信你应该将你的值作为bat文件中的命令行参数传递。当您在控制台中运行它时,项目将不会获得注释行参数,因此它将处于调试模式

#1


0  

I believe you should pass your value as a command-line argument in your bat file. When you run it in console the project will not get the comment-line argument, therefore it will be in debug-mode

我相信你应该将你的值作为bat文件中的命令行参数传递。当您在控制台中运行它时,项目将不会获得注释行参数,因此它将处于调试模式