可以从控制台运行但不创建窗口的C#应用​​程序 [英] C# application that can be run from console but does not create a window

查看:86
本文介绍了可以从控制台运行但不创建窗口的C#应用​​程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个C#应用程序,当您从控制台窗口运行它时它的行为类似于控制台应用程序(并且可以将文本输出到控制台),但是在外部运行时不创建控制台窗口。我做了一些研究,通常的建议是创建一个C#控制台应用程序,然后将其项目类型更改为Windows应用程序。但是,这会阻止从控制台运行时写入Console.WriteLine消息。

I'd like to create a C# application that behaves like a console application when you run it from a console window (and can output text to the console) but that does not create a console window when run externally. I've done a bit of research and the usual suggestion is to create a C# console application, then change its project type to a Windows Application. However that stops the Console.WriteLine messages from being written when run from a console.

有没有办法运行这样的程序?我不需要它来接受任何控制台输入。也许还有其他方法可以使我从控制台运行时显示文本?

Is there any way to run a program like this? I don't need it to take any console input. Perhaps there's some other method I can use to get text to display when run from the console?

推荐答案


所以所有控制台输出在提示后都会奇怪地显示

so all the console output shows up oddly after the prompt

在实践中使用AttachConsole()效果不佳。命令处理器查看您的.exe文件并执行两项不同的操作,具体取决于它是控制台模式应用程序还是GUI应用程序。如果它是控制台模式的应用程序,则它等待退出程序,然后显示提示。如果它是GUI应用程序,则它等待,它假定程序将显示其自己的窗口。

Using AttachConsole() just doesn't work that well in practice. The command processor looks at your .exe file and does two different things, depending if it is a console mode app or a GUI app. If it is console mode app then it waits for the program to exit, then displays the prompt. If it is a GUI app then it doesn't wait, it assumes that the program will display its own window.

发生了什么,它在启动程序后显示提示并等待输入。现在,您的Console.Write()输出与命令处理器的输出混合。您还会遇到Console.ReadLine()问题,用户键入的第一行将转到命令处理器,而不是您。

Which is what happened, it displayed the prompt after starting your program and waits for input. Your Console.Write() output now intermingles with the command processor output. You'll also have a problem with Console.ReadLine(), the first line that the user types goes to the command processor, not to you. Greatly confuzzling the user.

解决方法是以不同的方式启动程序。您输入

The workaround is to start your program differently. You have type

  start /wait yourapp.exe args...

这使命令处理器等待程序退出,就像在控制台模式应用程序中一样。

Which makes the command processor wait for your program to exit, just like it would for a console mode app.

这不是很实际,您的应用程序用户根本不会考虑使用 start / wait 。对此没有简单的解决方法,您需要使用AllocConsole()。如果这很麻烦,那么您只需要创建另一个小的EXE项目即控制台应用即可。这将通过命令行启动GUI应用程序。如果使用与GUI exe相同的名称,但使用 .com 扩展名,则始终会首先找到它。它可以看起来像这样,适用于任何GUI应用程序:

This is not very practical, the user of your app just won't think to use start /wait. No simple fix for this, you'll need to use AllocConsole(). If that's a deal breaker then you'll just need to create another little EXE project that's a console app. Which starts your GUI app, passing the command line. If you give it the same name as your GUI exe but give the .com filename extension then it will always be found first. It can look like this, works for any gui app:

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

class Program {
    static void Main(string[] args) {
        try {
            var path = Assembly.GetExecutingAssembly().Location;
            var dir = Path.GetDirectoryName(path);
            var file = Path.GetFileNameWithoutExtension(path);
            path = Path.Combine(dir, file + ".exe");

            var prc = Process.Start(path, Environment.CommandLine);
            if (args.Length > 0) prc.WaitForExit();
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Environment.Exit(1);
        }
    }
}

这篇关于可以从控制台运行但不创建窗口的C#应用​​程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆