繁体   English   中英

Visual Studio 2013 - 如何在控制台中查看输出?

[英]Visual Studio 2013 - How do I view output in the console?

我是C#和VS的新手,我只是尝试使用Console.WriteLine(...)打印一行,但它只显示在命令提示符下。 有没有办法在输出窗口中显示输出?

编辑:这是一个控制台应用程序。

另外,如何访问命令行以运行程序? 我只能弄清楚如何使用F5运行,但如果我需要输入参数,这将无法工作。

如果是ConsoleApplication,则Console.WriteLine将编写控制台。 如果使用Debug.Print ,它将打印到底部的Output选项卡。

如果要添加命令行参数,可以在项目属性中找到它。 单击Project -> [YourProjectName] Properties... -> Debug -> Start Options -> Command line arguments 此处的文本将在运行时传递给您的应用程序。 您也可以在构建它之后运行它,方法是在构建它之后,通过cmd或者您喜欢的方式运行它来运行bin\\Releasebin\\Debug文件夹。 我发现用这种方式测试各种参数比用每次设置命令行参数更容易。

是的,我也遇到了这个问题,这是VS2012的前两天。 我的控制台输出在哪里? 它闪烁并消失。 像有用的例子一样神秘

https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

好吧,确实是@martynaspikunas ..技巧你可以用Debug.WriteLine()代替Console.WriteLine()来在IDE中看到它。 它会留在那里,很好。

但有时你必须改变现有代码中的很多地方才能做到这一点。

我确实找到了一些替代品......单身怎么样

Console.ReadKey();

在Program.cs? 控制台会等你,它可以滚动..

我还想在Winforms上下文中使用Console输出:

 class MyLogger : System.IO.TextWriter
    {
        private RichTextBox rtb;
        public MyLogger(RichTextBox rtb) { this.rtb = rtb; }
        public override Encoding Encoding { get { return null; } }
        public override void Write(char value)
        {
            if (value != '\r') rtb.AppendText(new string(value, 1));
        }
    }

主窗体类之后添加此类。 然后在调用InitializeComponent()之后使用构造函数中的以下redirect语句将其插入:

 Console.SetOut(new MyLogger(richTextBox1));

因此,所有Console.WriteLine()都将出现在richTextBox中。

有时我会使用它重定向到List以便稍后报告控制台,或者将其转储到文本文件中。

注意:2010年,Hans Passant将MyLogger代码片段放在这里,

将控制台输出绑定到RichEdit

这是一个保持控制台及其输出的简单技巧:

int main ()
{
    cout << "Hello World" << endl ;

    // Add this line of code before your return statement and the console will stay up
    getchar() ;  

    return 0;
}
using System;
#region Write to Console
/*2 ways to write to console
 concatenation
 place holder syntax - most preferred
 Please note that C# is case sensitive language.
*/
#region
namespace _2__CShrp_Read_and_Write
{
    class Program
    {
        static void Main(string[] args)
        {

            // Prompt the user for his name
            Console.WriteLine("Please enter your name");

            // Read the name from console
            string UserName = Console.ReadLine();
            // Concatenate name with hello word and print
            //Console.WriteLine("Hello " + UserName);

            //place holder syntax
            //what goes in the place holder{0}
            //what ever you pass after the comma i.e. UserName
            Console.WriteLine("Hello {0}", UserName);
            Console.ReadLine();
        }
    }
}
I hope this helps

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM