C# 如何使用控制台应用程序显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14089978/
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-10 10:38:19  来源:igfitidea点击:

How to Display result with Console Application

c#.netconsoleoutputconsole-application

提问by Hamid

I write a class in C# and I want to display in console. But i can't display it.

我用 C# 编写了一个类,我想在控制台中显示。但我无法显示它。

My program doesn't have any errors, which means the program runs it but i can't see a result :(

我的程序没有任何错误,这意味着程序运行了它,但我看不到结果:(

Please help me regarding this problem.

请帮助我解决这个问题。

回答by Tilak

You can use Console.WriteLine(Object)to print the output, and Console.Read()to wait for user input.

您可以使用Console.WriteLine(Object)打印输出,并使用Console.Read()等待用户输入。

Console.WriteLine("Hello world");
Console.WriteLine("Press any key to exit.");
Console.Read();

Without Console.Read, sometimes output just comes, and program exits in a flash. So Output cannot be seen/verified easily.

没有Console.Read,有时输出刚好来,程序一闪就退出。因此无法轻松查看/验证输出。

回答by Javed Akram

Press CTRL+F5to see your output. This will wait the console screen until you press any key.

CTRL+F5查看您的输出。这将等待控制台屏幕,直到您按任意键。

回答by Kyrylo M

On MSDNyou can find basic guide how to create console applications and how to output results.

在 MSDN 上,您可以找到如何创建控制台应用程序以及如何输出结果的基本指南。

回答by jonprasetyo

No Problem, I get what you are asking...

没问题,我明白你的要求......

In regards to Microsoft Visual Studio 2010

关于 Microsoft Visual Studio 2010

  1. Write Console.WriteLine("Where are you console?");somewhere in your code - make sure you will definitely see it..

  2. Press Debug button (or the play button)

  3. In the Microsoft Visual Studio, Go to Debug -> Windows -> Output

  1. Console.WriteLine("Where are you console?");在您的代码中写下某处 - 确保您一定会看到它..

  2. 按调试按钮(或播放按钮)

  3. 在 Microsoft Visual Studio 中,转到调试 -> Windows -> 输出

A small 'Output' Windows should pop up and you can see your console write statements! - Obviously you gotta run the code and it will appear.

应该会弹出一个小的“输出”窗口,您可以看到您的控制台写入语句!- 显然你必须运行代码,它会出现。

Hope it helps!

希望能帮助到你!

回答by Emre Guldogan

Your main structure must be in "Windows Form" architecture. So, try to attach parent (base) process, such as:

您的主要结构必须是“Windows 窗体”架构。因此,尝试附加父(基)进程,例如:

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

        [STAThread]
        static void Main(string[] args)
        {
            if (Environment.UserInteractive) // on Console..
            {
                // redirect console output to parent process;
                // must be before any calls to Console.WriteLine()
                AttachConsole(ATTACH_PARENT_PROCESS);

                // to demonstrate where the console output is going
                int argCount = args == null ? 0 : args.Length;
                Console.WriteLine("nYou specified {0} arguments:", argCount);
                for (int i = 0; i < argCount; i++)
                {
                    Console.WriteLine("  {0}", args[i]);
                }
            }
            else
            {
                // launch the WinForms application like normal
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

(or write a console app from scratch)

(或从头开始编写控制台应用程序)