Seeing the console's output in Visual Studio 2010?

后端 未结 10 636
不思量自难忘°
不思量自难忘° 2020-12-12 12:15

I am writing a simple C# program with some outputs (Console.WriteLine(\"...\");). The problem is, each time I run it, I cannot see the program\'s output in the

相关标签:
10条回答
  • 2020-12-12 13:12

    Add a Console.Read(); at the end of your program. It'll keep the application from closing, and you can see its output that way.

    This is a console application I just dug up that stops after processing but before exiting:

    class Program
    {
        static void Main(string[] args)
        {
            DummyObjectList dol = new DummyObjectList(2);
            dol.Add(new DummyObject("test1", (Decimal)25.36));
            dol.Add(new DummyObject("test2", (Decimal)0.698));
            XmlSerializer dolxs = new XmlSerializer(typeof(DummyObjectList));
            dolxs.Serialize(Console.Out, dol);
    
            Console.WriteLine(string.Empty);
            Console.WriteLine(string.Empty);
    
            List<DummyObject> dolist = new List<DummyObject>(2);
            dolist.Add(new DummyObject("test1", (Decimal)25.36));
            dolist.Add(new DummyObject("test2", (Decimal)0.698));
            XmlSerializer dolistxs = new XmlSerializer(typeof(List<DummyObject>));
            dolistxs.Serialize(Console.Out, dolist);
            Console.Read(); //  <--- Right here
        }
    }
    

    Alternatively, you can simply add a breakpoint on the last line.

    0 讨论(0)
  • 2020-12-12 13:16

    System.Diagnostics.Debug.WriteLine() will work, but you have to be looking in the right place for the output. In Visual Studio 2010, on the menu bar, click Debug -> Windows -> Output. Now, at the bottom of the screen docked next to your error list, there should be an output tab. Click it and double check it's showing output from the debug stream on the dropdown list.

    P.S.: I think the output window shows on a fresh install, but I can't remember. If it doesn't, or if you closed it by accident, follow these instructions.

    0 讨论(0)
  • 2020-12-12 13:17

    In Program.cs, between:

    static int Main(string[] agrs)
    {
    

    and the rest of your code, add:

    #if DEBUG
        int rtn = Main2(args);
        Console.WriteLine("return " + rtn);
        Console.WriteLine("ENTER to continue.");
        Console.Read();
        return rtn;
    }
    
    static int Main2(string[] args)
    {
    #endif
    
    0 讨论(0)
  • 2020-12-12 13:20

    Here are a couple of things to check:

    1. For console.Write/WriteLine, your app must be a console application. (right-click the project in Solution Explorer, choose Properties, and look at the "Output Type" combo in the Application Tab -- should be "Console Application" (note, if you really need a windows application or a class library, don't change this to Console App just to get the Console.WriteLine).

    2. You could use System.Diagnostics.Debug.WriteLine to write to the output window (to show the output window in VS, got to View | Output) Note that these writes will only occur in a build where the DEBUG conditional is defined (by default, debug builds define this, and release builds do not)

    3. You could use System.Diagnostics.Trace.Writeline if you want to be able to write to configurable "listeners" in non-debug builds. (by default, this writes to the Output Window in Visual Studio, just like Debug.Writeline)

    0 讨论(0)
提交回复
热议问题