简体   繁体   English

为什么 Console.Writeline、Console.Write 在 Visual Studio Express 中不起作用?

[英]Why doesn't Console.Writeline, Console.Write work in Visual Studio Express?

I just open a console application and I type我只是打开一个控制台应用程序并输入

Console.WriteLine("Test");

But the output window doesn't show this.但是输出窗口没有显示这个。 I go to the output window with Ctrl + W , O .我用Ctrl + WO转到输出窗口。

But nothing shows up when I run my program.但是当我运行我的程序时什么都没有出现。 Am I nuts or is this not supported in Visual Studio 2010 Express ?我是疯了还是Visual Studio 2010 Express不支持这个?

Console.WriteLine将您的输出写入由您的应用程序打开的控制台窗口(想想当您打开命令提示符时出现的带有白色文本的黑色窗口。)改为尝试System.Diagnostics.Debug.WriteLine

No satisfactory answers provided.没有提供满意的答案。

System.Diagnostics.Debug.WriteLine() will write messages to the Output:debug window, but so much crap is constantly dumped into that window by every process under the sun, it is like finding a needle in a haystack to find YOUR messages. System.Diagnostics.Debug.WriteLine()会将消息写入 Output:debug 窗口,但是每个进程都在不断向该窗口倾倒如此多的废话,就像大海捞针一样寻找您的消息。

Console.WriteLine() does not write to any window in Visual Studio. Console.WriteLine()不会写入 Visual Studio 中的任何窗口。 I guess it will only write to the application console if your application creates a console in the first place, ie if it is a Console application.我想它只会写入应用程序控制台,如果您的应用程序首先创建一个控制台,即如果它是一个控制台应用程序。

Why is the tooling making the simple task of having your web application server side code .cs code write some debug messages into a window which is swamped with crap making it almost impossible to find your information?为什么该工具可以让您的 Web 应用程序服务器端代码 .cs 代码将一些调试消息写入一个充满垃圾的窗口,从而几乎不可能找到您的信息?

Go to properties in you own project in Solution Explorer window and choose application type and look for Output Type and change it's value to Console Application .Solution Explorer窗口中转到您自己项目中的属性并选择应用程序类型并查找Output Type并将其值更改为Console Application This will make console screen besides your form.除了您的表单之外,这还将制作控制台屏幕。 If you close console screen, your form will be closed too.如果您关闭控制台屏幕,您的表单也将关闭。

Good luck.祝你好运。

Perhaps the console is clearing.也许控制台正在清除。 Try:尝试:

Console.WriteLine("Test");
Console.ReadLine();

And it will hopefully stay there until you press enter.并且希望它会一直留在那里,直到您按 Enter 键。

或者您可以通过CTRL+F5进行调试,这将在执行最后一行后打开 ConsoleWindow 等待,直到您按下键。

It's more than likely because you've used Console in the namespace.这很有可能是因为您在命名空间中使用了 Console。 For example like this:例如像这样:

namespace XYZApplication.Console
{
    class Program
    {
        static void Main(string[] args)
       {
            //Some code;             
       }
    }
}

Try removing it from the namespace or use the full namespace instead ie尝试从命名空间中删除它使用完整的命名空间,即

   System.Console.Writeline("abc");

Try ctrl+F5, it will hold your Screen until you press any key.试试 ctrl+F5,它会一直按住你的屏幕直到你按任意键。 Regards问候

In Winforms app, both methods:在 Winforms 应用程序中,两种方法:

System.Diagnostics.Debug.WriteLine("my string")

and

System.Console.WriteLine("my string")

write to the output window.写入输出窗口。

In AspNetCore app, only System.Diagnostics.Debug.WriteLine("my string") writes to the output window.在 AspNetCore 应用程序中,只有System.Diagnostics.Debug.WriteLine("my string")写入输出窗口。

The output window isn't the console.输出窗口不是控制台。 Try the methods in System.Diagnostics.Debug尝试System.Diagnostics.Debug的方法

Right click on the project in solution-explorer and click "clean".右键单击解决方案资源管理器中的项目,然后单击“清理”。

Now run F5现在运行F5

Make sure the code is as below:确保代码如下:

Console.WriteLine("TEST");
Console.ReadLine();

I run into a similar problem while running a Unit Test.我在运行单元测试时遇到了类似的问题。 Console.WriteLine() did not write anything into the VS Output Window. Console.WriteLine()没有向 VS 输出窗口写入任何内容。

Using System.Diagnostics.Debug.WriteLine() solved the problem.使用System.Diagnostics.Debug.WriteLine()解决了这个问题。

If you use Ctrl-F5 (start without debugging) it will leave the console window open with a message "Press any key to continue".如果您使用 Ctrl-F5(在不调试的情况下启动),它将使控制台窗口保持打开状态,并显示一条消息“按任意键继续”。 That's the easiest way to keep the console window from closing so you can see the console output.这是防止控制台窗口关闭的最简单方法,以便您可以看到控制台输出。

转到调试菜单并选择选项并取消选中“将所有输出窗口文本重定向到立即窗口”

如果您正在开发命令行应用程序,您还可以在代码末尾使用Console.ReadLine()以在关闭控制台窗口之前等待“Enter”按键,以便您可以读取输出。

using System.Diagnostics;


Trace.WriteLine("This line will show on Output window"); 
Trace.Flush();

This works on Microsoft Team Explorer for Visual Studio 2013这适用于 Visual Studio 2013 的 Microsoft Team Explorer

Refer to microsoft.com 请参阅 microsoft.com

Workaround I found:我发现的解决方法:

Press Ctrl + Alt + I or navigate to "Debug Tab" ---> "Windows" ---> "Immediate".Ctrl + Alt + I或导航到“调试选项卡”--->“Windows”--->“立即”。

In your code write:在你的代码中写:

Trace.WriteLine("This is one of the workaround");

Visual Studio 2017 的输出窗口有一个名为Show output from的菜单,在我的情况下, ASP.NET Core Web Server是选择以查看打印输出的选项,因为我将它设置为Build所以我遇到了这个问题我在运行时没有看到打印出来的行。

There are 2 possible problems are:有2个可能的问题是:

  • Firstly, you have not used the using System which should be before writing code that uses "System class", as Console.WriteLine()首先,在编写使用“系统类”的代码之前,您没有使用using System ,如Console.WriteLine()
  • Secondly, you have not coded what happens after the Console displays "Test"其次,您还没有对控制台显示“测试”后发生的情况进行编码

The possible solution will be:可能的解决方案是:

using System;

namespace Test
{
    public static Main()
    {
        //Print to the console
        Console.WriteLine("Test");

        //Allow user to read output
        Console.ReadKey();
    }
}

It is also strategic to code Console.Write("Press any key to exit...");编写代码Console.Write("Press any key to exit..."); on the line that precedes the Console.ReadKey();Console.ReadKey();之前的行上Console.ReadKey(); to make the user aware that the program is ending, he/she must press any key to exit.为了让用户知道程序正在结束,他/她必须按任意键退出。

None of the answers here worked for me!!这里没有一个答案对我有用!! Most of these people here are stuck in Windows Desktop Application Consoleland .这里的大多数人都被困在 Windows 桌面应用程序控制台领域 If you are a web developer using ASP.NET in Visual Studio and do not see any console or debug text, here is how to fix that:如果您是在 Visual Studio 中使用ASP.NETweb developer ,并且没有看到任何控制台或调试文本,则可以通过以下方式解决该问题:

  1. Paste the following two tests into your code so it runs both lines.将以下两个测试粘贴到您的代码中,使其运行两行。 These are tests for the output window:这些是输出窗口的测试:

    System.Console.WriteLine($"hello console!"); System.Console.WriteLine($"hello console!");

    System.Diagnostics.Debug.WriteLine($"hello debugger!"); System.Diagnostics.Debug.WriteLine($"你好调试器!");

  2. In Visual Studio choose VIEW > OUTPUT.在 Visual Studio 中选择 VIEW > OUTPUT。 You will see the results above in this output window after changing two settings below.更改以下两个设置后,您将在此输出窗口中看到上面的结果。

  3. When NOT DEBUGGING, in the OUTPUT window at the top under "Show Output From" choose: " YourProjectName - ASP.NET CORE Web Server ".当 NOT DEBUGGING 时,在“Show Output From”下顶部的 OUTPUT 窗口中选择:“ YourProjectName - ASP.NET CORE Web Server ”。 Run your code.运行你的代码。 You should see the "Console" text above.您应该会看到上面的“控制台”文本。

  4. When DEBUGGING, in the OUTPUT window at the top under "Show Output From" choose: " Debugger ". DEBUGGING 时,在顶部的 OUTPUT 窗口中的“Show Output From”下选择:“ Debugger ”。 Run your code in debug mode.在调试模式下运行您的代码。 You should see the "Debug" text above.您应该会看到上面的“调试”文本。

Change the execution mode to "Self Hosted".将执行模式更改为“自托管”。 In this case, the execution console will appear and all the messages will be displayed.在这种情况下,将出现执行控制台并显示所有消息。

Console.Writeline()显示在调试输出中(调试 => Windows => 输出)。

暂无
暂无

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

相关问题 为什么 Console.WriteLine 在 Visual Studio Code 中不起作用? - Why doesn't Console.WriteLine work in Visual Studio Code? 为什么我们不能在没有方法的情况下在 class 中使用 console.write 或 console.writeLine - Why we can't use console.write or console.writeLine in class without Method 为什么 console.writeline 在 Visual Studio for Mac 中不起作用? - Why isn't console.writeline working in Visual Studio for Mac? 一个Console.WriteLine(“text”+ function(parameter)); 不工作,但这条指令分为两个Console.Write工作正常 - A Console.WriteLine(“text” + function(parameter)); don't work but this instruction split into two Console.Write work fine 为什么Console.WriteLine在Visual Studio中运行无需调试就不会打印任何内容? - Why doesn't Console.WriteLine print anything in Visual Studio with Run without Debugging? 为什么Microsoft重载方法Console.Write()和Console.WriteLine()? C# - Why Microsoft overload methods Console.Write() and Console.WriteLine() ? C# 为什么在我编写 Console.WriteLine 时 Visual Studio 不报错? - Why does Visual Studio not complain when I write Console.WriteLine? 在 Visual Studio 中找不到 Console.Writeline - Cannot find Console.Writeline in Visual Studio 为什么我们不能在if语句中编写Console.Writeline()? - Why can't we write Console.Writeline() in if statement? 使用 Console.Writeline() 或 Console.Write() 时,在多线程 C# 控制台应用程序中很少挂起 - Infrequent hangs in a multi-threaded C# console application when using Console.Writeline() or Console.Write()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM