C# Console.Writeline() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1240867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:04:37  来源:igfitidea点击:

Console.Writeline() not working

c#command-lineconsole

提问by novacara

I'm creating a c# winforms project that can be run as a GUI or can be operated from the command line. Currently, I can process command line input and arguments. I can run the program from command line and I can use the program to process the arguments. But Console.Writeline() does absolutely nothing. Any clue why that could be?

我正在创建可以作为 GUI 运行或可以从命令行操作的 ac# winforms 项目。目前,我可以处理命令行输入和参数。我可以从命令行运行该程序,我可以使用该程序来处理参数。但是 Console.Writeline() 完全没有任何作用。任何线索为什么会这样?

采纳答案by Thorarin

For the most part, see the answer here. However, I would like to point out the existence of the FreeConsole()API call, which allows you to gracefully close the console.

大多数情况下,请参阅此处答案。但是,我想指出FreeConsole()API 调用的存在,它允许您优雅地关闭控制台。

[DllImport("kernel32.dll")]
static extern int FreeConsole() 

One thing I'd like to note: you may see some weirdness of a command prompt appearing in front of your console output, if you're launching from an existing console and attaching to that with AttachConsole(as opposed to AllocConsole).

我要注意的一件事是:如果您从现有控制台启动并使用AttachConsole(而不是AllocConsole)附加到该控制台,您可能会看到控制台输出前面出现的命令提示符有些奇怪。

This is a timing issue which is hard to work around. If that's a problem, set your application to be a Console application like the others suggested. It will have the effect of no command prompt appearing until the application closes however, which might not be what you want if you're opening a winform.

这是一个很难解决的时间问题。如果这是一个问题,请将您的应用程序设置为像其他人建议的那样的控制台应用程序。它的作用是在应用程序关闭之前不出现命令提示符,但是,如果您打开一个 winform,这可能不是您想要的。

In response to your comment: it's either AttachConsoleorAllocConsole. The example I linked tries to attach to an existing console first. If that fails (most likely because it doesn't exist), it creates a new console window instead.

在回答您的评论:它要么AttachConsoleAllocConsole。我链接的示例首先尝试附加到现有控制台。如果失败(很可能是因为它不存在),它会创建一个新的控制台窗口。

If you find a way to have the best of both worlds in terms of command-line behavior and GUI interactive mode, please let me know. I haven't done any in-depth searching for a solution, but I have a few minor apps that would benefit.

如果您找到了一种在命令行行为和 GUI 交互模式方面两全其美的方法,请告诉我。我没有对解决方案进行任何深入的搜索,但我有一些小应用程序可以从中受益。

By the way: if you plan on using pipeson your command line (redirecting output to a file for example), that won't work like this unfortunately.

顺便说一句:如果您打算在命令行上使用管道(例如,将输出重定向到文件),那么不幸的是,这不会像这样工作。

回答by Jhonny D. Cano -Leftware-

It has to do with the project type as you configure it on App tab on the properties of the project, you have 3 options:

当您在项目属性的 App 选项卡上配置它时,它与项目类型有关,您有 3 个选项:

  • Console Application
  • Win Application
  • Class Library
  • 控制台应用程序
  • 赢申请
  • 类库

If you work with Win Application, however, you can also process the command line arguments, because these are received on the Main function.

但是,如果您使用 Win Application,您还可以处理命令行参数,因为这些参数是在 Main 函数上接收的。

回答by Vilx-

The project must be set to compile to a console application for console function to work. On the other hand, it will then always have a console window, even when run as GUI. I do not know of a workaround for this (but would be interested to hear of it if there is one).

必须将项目设置为编译为控制台应用程序才能使控制台功能正常工作。另一方面,即使作为 GUI 运行,它也将始终有一个控制台窗口。我不知道有什么解决方法(但如果有的话,我很想知道)。

回答by Andrew Hare

This is because you have written a winforms app - this means that System.Console.Out(i.e. the standard output stream) is set to Stream.Null. This means that any calls to that stream will silently fail.

这是因为您编写了一个 winforms 应用程序 - 这意味着System.Console.Out(即标准输出流)设置为Stream.Null. 这意味着对该流的任何调用都将以静默方式失败。

You are able to process input from the command line because they come from a different stream. The moral of the story is that you can have a winforms app, or a command line app, but not both at once.

您可以处理来自命令行的输入,因为它们来自不同的流。这个故事的寓意是,您可以拥有一个 winforms 应用程序或一个命令行应用程序,但不能同时拥有。

回答by gls123

You can enable the console in a windows forms app using the following DllImports:

您可以使用以下 DllImports 在 Windows 窗体应用程序中启用控制台:

    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);
    private const int ATTACH_PARENT_PROCESS = -1;

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern int FreeConsole();

You can then enable the console using:

然后,您可以使用以下方法启用控制台:

    AttachConsole(ATTACH_PARENT_PROCESS);

And you can disable it using:

您可以使用以下方法禁用它:

     FreeConsole();

回答by Antonio Nicolaas Teyken

I did this in a background thread that is running a function from a dynamically loaded DLL (reflection)

我在后台线程中执行此操作,该线程从动态加载的 DLL 运行函数(反射)

AllocConsole();
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);

StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
//
//Console writing here
//
FreeConsole();

My console was blank until I added the Console.SetOut(), I am not sure you need to call GetStdHandle()

在我添加 Console.SetOut() 之前,我的控制台是空白的,我不确定您是否需要调用 GetStdHandle()

回答by user1251007

You might need to change the application type. This can be done by either changing it via Application Properties in the GUI (as explained in the answerfrom ConsultUtah) or you can edit the .csprojfile:

您可能需要更改应用程序类型。这可以通过在 GUI 中通过应用程序属性更改它来完成(如ConsultUtah的回答中所述),或者您可以编辑.csproj文件

Winform/WPF:

Winform/WPF:

<OutputType>WinExe</OutputType>

Console (this is what you need for console.writeline()to be working):

控制台(这是您console.writeline()工作所需的):

<OutputType>Exe</OutputType>

Dll (including web applications):

Dll(包括网络应用程序):

<OutputType>Library</OutputType>