简体   繁体   English

如何在运行时更改应用程序输出类型

[英]How to Change Application Output Type During Runtime

I am trying to add command-line support for a desktop application I built so that I can run its commands from elsewhere without display the UI. 我正在尝试为构建的桌面应用程序添加命令行支持,以便可以在其他地方运行其命令而无需显示UI。 This is approximately what my Main method looks like: 这大约是我的Main方法的样子:

[STAThread]
private static void Main(string[] args)
{
    // Run the desktop application
    if (args.Length == 0)
    {
        AppDomain.CurrentDomain.ProcessExit += OnProcessExit;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var mainForm = new Form1();
        Application.Run(mainForm);
        mainForm.BringToFront();
    }
    // Restart the session and log in console
    else if(args[0] == "restartsession")
    {
        Console.WriteLine("Restarting session...");
    }
    // argument not recognised (log to console)
    else
    {
        Console.WriteLine($"Argument \"{args[0]}\" not recognised");
    }
}

This appears to work just fine, apart from the fact that the Console window is not displayed when calling Console.WriteLine() . 除了在调用Console.WriteLine()时不显示“控制台”窗口的事实之外,这似乎还可以正常工作。

This is due to my project's output type being set to Windows Application (I set this in properties). 这是由于我的项目的输出类型设置为Windows Application (我在属性中设置了它)。 If I change it to Console Application the console window appears with my logging, but then it also displays when opening the "UI" desktop version (ie running the app without any arguments). 如果将其更改为“ Console Application则控制台窗口随日志记录一起出现,但是在打开“ UI”桌面版本(即,不带任何参数运行该应用程序)时也会显示该窗口。

Is there a way to change the output type depending on the arguments passed into Main() ? 有没有一种方法可以根据传递给Main()的参数来更改输出类型? For example something like this perhaps: 例如如下所示:

private static void Main(string[] args)
{
    if (args.Length == 0)
    {
        Application.OutputType = WindowsApplication
    }
    else if(args[0] == "restartsession")
    {
        Application.OutputType = ConsoleApplication
    }
}

I was able to find this link but it appears to be old and not relevant any more. 我能够找到此链接,但它似乎已经很旧,不再相关。 Is there some other way of achieving this? 还有其他方法可以做到这一点吗?

As Leppie suggested in a comment, this is not possible and I will have to build 2 separate applications - one for the desktop and one for the command-line. 正如Leppie在评论中建议的那样,这是不可能的,我将必须构建2个独立的应用程序-一个用于桌面,一个用于命令行。

A possible solution for this would be to compartmentalise the RestartSession functions into a dll and then have 2 small exes calling the same dll (one for the desktop and one for the console) 一个可能的解决方案是将RestartSession函数分隔为一个dll ,然后让2个小exe调用同一dll(一个用于桌面,一个用于控制台)。

Leppie has the right answer in the comments. 勒皮在评论中有正确的答案。 For scenarios like this, your actual application project should be very small. 对于这样的情况,您的实际应用项目应该是非常小的。 It should include only things that are specific to that application type. 它应仅包括特定于该应用程序类型的内容。

All of your business logic, application shared initialization logic, etc. should be moved to a different project which is consumed by both. 您所有的业务逻辑,应用程序共享的初始化逻辑等都应移至两者都使用的不同项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM