2014-02-25 103 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 
相關問題