What's the equivalent of System.out.println() in C#/Silverlight? - c#

I am developing some projects in C# and Silverlight.
I am trying to print lines of code in order to debug, but Console.Write() doesn't seem to work.
I've created a Silverlight Application, not a Console Application where Console.Write() did work. How can I print in a console or in the output window in a Silverlight Application project?

Use System.Diagnostics.Debug.Write to print in the debug output window.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.write.aspx

Using System.Diagnostics.Debug.WriteLine(); like Fox32 and VoodooChild mentioned did not work for me initially. Apparently my debugging wasn't working at all (breakpoints did not hit for example).
I was able to fix the debugging by using IE instead of FireFox (see Debugging Silverlight not hitting breakpoints)
As soon as that was fixed, the System.Diagnostics.Debug.WriteLine(); worked perfectly :)

Related

Is it possible to capture the console screen under Windows?

I want to make a GUI Windows application that can run console applications. When this happens, the console window should not be shown. Instead its content should be shown in a visual component (memo/richedit). This component should show exactly the same content which would appear in the console window, even the color of the text and its background should be displayed. So this visual component should work exactly as a console window. I know that the standard output can be captured but many console applications do not use it. Is it possible to capture the output of a console application this way? Are there Windows API calls that can handle this?
I use Delphi XE2 but C# code would also be helpful.
You have to run the console mode program with stdout redirection to a pipe that your Delphi program will create. Your Delphi program can read the pipe to get the console mode program output and do whatever it needs to. By the way, this works not only with Delphi but also with any language able to create pipe and run program with I/O redirection.
If you need Delphi code to do that, have a look at this reference.
There is a ready-to-run component on GitHub: DosCommand
The Demo shows two ways how to do what you describe.
I am not sure if it works for older versions like XE2, but at least you can give it a try.
Traditionally you would call CreateProcess with stdin/stdout set to pipes you created. This should work for most programs but not for anything that uses a ncurses style "GUI" and you also lose the color information. An example can be found on MSDN.
Windows 10 (1809?) added support for pseudoconsoles. This is used by the new Terminal application and is your best bet for full console compatibility.
The last alternative is to inject into the child process and hook WriteFile, ReadFile and all the console functions but this is ugly and error-prone.

Debugging in monogame

How do you print or output text in Monogame?
I googled how to display text in monogame and was led to this:
Debug.WriteLine
Which says: "By default, the output is written to an instance of DefaultTraceListener."(and that page just confused me more).
So, if someone could direct me to a method of displaying DefaultTraceListener, or another method of outputting text in monogame, I would appreciate it.
I found it!
Using Debug.WriteLine writes to the debugger, which is in the output window in Visual Studio(by default at the bottom). It appears when you close the program(press F5 to start, Esc to close) by default in an OpenGL project.
If you like, you can use Console.WriteLine like you would in a normal C# console application, assuming you're developing a desktop application. There are a couple of steps.
Open the Properties for your MonoGame project
Select the Application tab
Change the Output Type to Console Application.
Your application should run as normal, only a console window should appear when you start the game.
Alternatively, you can use Debug.WriteLine, which will write to the output window in Visual Studio (it should appear when you start debugging your game).
If you use the standard Debug.WriteLine or Trace.WriteLine, then output goes to the default trace listener which can be viewed in the Visual Studio output window. Outside of Visual Studio, you can use programs such as DebugView (SysInternals) or LogFusion (Binary Fortress) to display the output. DebugView even has a feature for viewing debug output from a remote machine.
There are other trace listeners that can send output to a file, or to the Windows event log, or you can write your own trace listeners fairly easily.
You could also consider using a ready-made logging framework such as NLog, which would give you a great deal of flexibility. I have found in practice that using NLog turns out to be a lot easier than the built in stuff in .NET, because of the way it lets you easily reconfigure things and control/filter the output in a much more flexible way.
I know this has been answered, but if anyone else stumbles upon this, you can also use Console.Write(thing in here); or Console.WriteLine(thing in here); to write to the console window. WriteLine adds a line ending and Write does not.

c# enable console without modify output type

I'm building a windows application, but I need also the features of the console.
If I change the output type from Windows Aapplication to Console Application it works fine, but it shows always the console.
It's possible enabling the console by code only in certain situations?
thanks in advance!
Perhaps this can be of any help:
How to run a C# console application with the console hidden
The person asking the question was running a WinForms app where he executed other things through a console application without showing it. Seems similar to your situation. Hope this helps! :)

Getting started with C#, how to Debug .NET 4.0 MVC3 Project?

I am trying to self teach myself C# and wondering if anyone can help me with what seems to be a basic C# question. I created a C# file with this code, started debugging but don’t see “Hello World” anywhere.
using System;
class Hello
{
static void Main() {
Console.WriteLine("hello, world");
}
}
http://msdn.microsoft.com/en-us/library/aa664628(v=vs.71).aspx
So I guess my question is this. Where should I expect to see “Hello World”? I have checked the Console and the Browser. Is there some setup that needs to be done to properly debug C# files. I am probably missing the big picture as to how C# works. I am use to PHP where I can just do something like this...
<?php
include 'my file';
echo 'my file included';
?>
Any help would be much appreciated. Thanks.
EDIT:
Thanks everybody for all of the help. You have all helped me understand and realize a number of things about C# / .NET. After extensive troubleshooting it is evident that the problem is not a mater of the debugging working, but the fact that my C# file doesn't appear to be properly hooked/included (not sure what its called in .NET terms) to the rest of the project. Anyways I am accepting keyboardP's answer as he answered first and technically gave me all the right answers. Also thanks to dasblinkenlight who was also extra helpful.
Additional Solution:
After insight from SO users. This article helped point me in the right direction.
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
I'm guessing it's because the command prompt window is immediately closing. You can avoid this by adding Console.ReadLine(); after your WriteLine statement. This will wait for you to press return before closing the prompt window.
Alternatively, assuming you're using Visual Studio, you can run the build without a debugger attached by pressing CTRL + F5.
Edit - Based on the extra information added that you're using ASP.NET and not a console application.
Firstly, what are you trying to achieve? If you want to output debug information, then you can Debug.WriteLine instead of Console.WriteLine
System.Diagnostics.Debug.WriteLine("Hello World");
This will output the text to the "Output" window at the bottom of Visual Studio (by default).
Edit 2 Since you just want to write random text to the page, you can use
HttpContext.Current.Response.Write("Hello World");
There are sometimes issues with Response.Write but it should be okay for what you want to do here.
Use breakpoints. Set a breakpoint at the end of your method by clicking in the "gutter" area. A red circle will appear that looks like this:
Now run your program in debug mode by clicking the button with the green triangle or pressing F5. The program will run, producing the output in the console (a separate window). Once it hits your breakpoint, you can examine the console for the output, like this:
You are reading a tutorial for Console Application, however you are trying to create a ASP.NET application. I would reccomend reading a tutorial for ASP.NET
Like many before said: it goes by too fast, so either use breakpoints, or use a Read...
You can also write to your Visual Studio output window with System.Diagnostics.Debug.Write
you need to put break point over the line which you want to debug
short cut to placing break point is ctrl D,n
then you can step over or step into the code with f10 and f11 function keys

Hide the console (completely) for a Console app, but only sometimes

I have a C# Console app than runs a pre-build step (to get NuGet Packages).
When I am debugging this, I want to pass in a parameter and show the console. When I am not debugging it I don't want to see it. I don't even want it to flash up there for a second.
I have found ways to hide it, after it has shown. But I can't find a way to never make it show unless I am willing to change it from a console app to a Windows app. (Which I would do if I could then find a way to show the Console when needed.)
Build as a Windows application and show the console when you need it. To show the console when needed use P/Invoke to call AllocConsole (pinvoke.net has the declaration you need).
(Console sub-system processes always get a console, their parent process's if there was one, otherwise a new one. This is the way Windows works at a deep level.)
Use FreeConsole WINAPI function:
http://pinvoke.net/default.aspx/kernel32/FreeConsole.html
Another solution is to simply switch to a WinForms application as project type. No console will be allocated then (and you do not need to show a form).

Categories

Resources