3

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.

8
  • It's a console application.
    – Aei
    Commented Feb 25, 2014 at 23:59
  • 1
    Please clarify your terms. What do you mean by "command line", as in, "but it only shows up in the command line". What do you mean by "console", as in "is there a way to make output show in the console instead?"?
    – Kirk Woll
    Commented Feb 26, 2014 at 0:03
  • @Aei, try Debug.WriteLine(); Commented Feb 26, 2014 at 0:09
  • @martynaspikunas The editor doesn't seem to recognize the name Debug.
    – Aei
    Commented Feb 26, 2014 at 0:12
  • 1
    You need to resolve namespace issues. With your right mouse button click on Debug class and choose resolve option. Commented Feb 26, 2014 at 0:13

4 Answers 4

10

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.

2

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

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.