简体   繁体   中英

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

I'm new to C# and VS and I'm just trying to print a line using Console.WriteLine(...) but it only shows up in the command prompt. Is there a way to make output show in the output window instead?

EDIT: It's a console application.

Also, how do I access the command line in order to run programs? I've only been able to figure out how to run with F5, but this won't work if I need to type in arguments.

If it's a ConsoleApplication then Console.WriteLine will write the console. If you use Debug.Print , it will print to the Output tab at the bottom.

If you want to add command line arguments, this can be found in the project properties. Click Project -> [YourProjectName] Properties... -> Debug -> Start Options -> Command line arguments . The text here will be passed to your application when it's run. You can also run it after it's built by running it out of the bin\\Release or bin\\Debug folder after you build it, through cmd or however you prefer. I find it easier to test various parameters this way rather than setting the command line arguments each time.

Yes I ran into this problem too, my first 2 days with VS2012. Where is my console output? it flashes and disappears. Mystified by usefull examples like

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

Ok, indeed @martynaspikunas .. trick you can replace Console.WriteLine() by Debug.WriteLine() to see it in the IDE. It will stay there, nice.

But sometimes you have to change a lot of places in existing code to do that.

I did find a few alternatives.. How about a single

Console.ReadKey();

in Program.cs ? The console will wait for you, it can scroll..

I also like to use the Console output in my Winforms contexts:

 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));
        }
    }

Add this class after your main form class. Then plug it in, using the following redirect statement in the constructor, after InitializeComponent() is called:

 Console.SetOut(new MyLogger(richTextBox1));

As a result of this, all your Console.WriteLine() will appear in the richTextBox.

Sometimes I use it to redirect to a List to report Console later, or dump it to a textfile.

Note: the MyLogger code snippet was put here by Hans Passant in 2010,

Bind Console Output to RichEdit

Here is a simple trick to keep the console and its output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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