【问题标题】:How to Display result with Console Application如何使用控制台应用程序显示结果
【发布时间】:2012-12-14 22:14:56
【问题描述】:

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

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

请帮我解决这个问题。

【问题讨论】:

  • 请在您的问题中包含代码。
  • ..请显示一些代码。
  • Console.WriteLine("Hello");

标签: c# .net console output console-application


【解决方案1】:

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

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

没有Console.Read,有时输出就来了,程序很快就退出了。因此输出无法轻易查看/验证。

【讨论】:

    【解决方案2】:

    没问题,我明白你在问什么......

    关于 Microsoft Visual Studio 2010

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

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

    3. 在 Microsoft Visual Studio 中,转到 Debug -> Windows -> Output

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

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我猜这意味着某个 IDE?
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        您的主要结构必须采用“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());
                    }
                }
            }
        }
        
        

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

        【讨论】:

          猜你喜欢
          • 2020-08-11
          • 2019-09-10
          • 1970-01-01
          • 1970-01-01
          • 2013-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-03
          相关资源
          最近更新 更多