2014-02-25 102 views
3

我是C#和VS的新手,我只是想用Console.WriteLine(...)打印一行,但只显示在命令提示符中。有没有办法让输出显示在输出窗口中?Visual Studio 2013 - 如何查看控制台中的输出?

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

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

+0

这是一个控制台应用程序。 – Aei

+1

请澄清一下您的条款。 “命令行”意味着什么,如“,但它只显示在命令行中”。 “控制台”是什么意思,如“有没有办法让输出显示在控制台上?”? –

+0

编辑澄清。 – Aei

回答

9

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

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

2

是的我也遇到过这个问题,我的第一个2天VS2012。我的控制台输出在哪里?它闪烁并消失。通过有用的例子迷惑像

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

好吧,确实@martynaspikunas ..招可以更换Console.WriteLine()通过的Debug.WriteLine()看到它在IDE中。它会留在那里,很好。

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

我没有找到一个几个选择..怎么样一个

Console.ReadKey(); 
在Program.cs中

?控制台会等你的,它可以滚动..

我也喜欢使用控制台输出在我的WinForms背景:

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()被调用:

Console.SetOut(new MyLogger(richTextBox1)); 

由于这一结果,所有的Console.WriteLine()将出现在RichTextBox中。

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

注:MyLogger代码片段被放到这里由Hans帕桑特在2010年,

Bind Console Output to RichEdit

0

下面是一个简单的技巧,以保持控制台和它的输出:

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

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

    return 0; 
} 
+1

这个答案是错误的,因为它使用C++而OP要求C# –

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 
相关问题