Outputing text to command line when using C# and Visual Studio - c#

I am running Visual Studio 2010 and using C#.
I want to be able to launch an executable from the command line and be able to output text to the command window I launched from.
I tried these but they did not work at all.
Anybody have any ideas?
System.Diagnostics.Debug.WriteLine("diag text");
Console.WriteLine("text");
Thank you very much!

First, make sure you are running a Console Application. Once you've confirmed that, at the end add this line:
Console.ReadLine();
without it your application will run, and close before you are able to see the console. Adding that line will make the application wait till you press a key to close.

Related

Hololens: no debug output in Visual Studio 2017

My task is very simple, I put in the Main method two lines:
Debug.Log("App START Debug.Log");
Debug.unityLogger.Log("App", "START Debug.unityLogger.Log");
and want to see these logs in the output if the app is running. If I set the run option as Releasethe app starts, but I don't see these logs. If I set the run option as Debug and try to start, I get an error popup:
Unable to activate Windows Store app ´Template3D_pzq3xp76mxafg!App´
and the app doesn't start at all.
EDIT: according to comment of #Perazim, I have also tried:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
No effect in Release and Debug mode.
EDIT 2:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
works, if I put it in other methods, but doesn't work in the Main() method.
Debugger.Log for the output to debugger. Debug.WriteLine prints to listeners. If there is no listeners, it will not work.
My application stopped writing output to the output window with the System.Diagnostics.Debug.Writeline in debug mode. This all happened after the latest Microsoft Updates.
I created a new application just to see if it was my current application, and the new application would not write to the output window either.
Then I decided to right click on the Output Pane in Visual Studio and what do I see, Program Output unchecked.
I checked this and my Debug.Writeline works again.
In unity you can use/see debug.log(); in Visual Studio you need to use Console.WriteLine();

Console immediately closes with CTRL+F5

In Visual Studio 2017, I'm using C# to create a basic hello world application.
CTRL+F5 (Start without debugging) immediately shuts down without showing the output.
I've followed this solution the second most upvoted answer
EDIT: Apparently this solution doesn't apply to C#
I've also tried resetting my settings and uninstalling visual studio. I don't want to add breakpoints or ReadLine.
Bottom line, I just want my program to not exit on CTRL+F5.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
}
}
}
Did you create this using the console app template? If not, try doing that.
You can check that it's set correctly by making sure that the output type (properties --> Application --> Output type) is set to 'Console Application' for this to work when pressing CTRL + F5
in a console app you need some sort of wait after you print to the screen or the app will exit. try adding Console.ReadLine(); it will then wait until you press enter to exit.
EDIT:
if you don't want to do that then run it from a command prompt to avoid the auto close behavior that Visual Studio adds.
Jay pointed out in a comment that maybe I used the wrong template. I don't know what template I used before, but I remade the application using the Console template and it works now.
Make sure that command line arguments have one line only. Check your start-up project Properties | Debug | Start options | Command line arguments.

How do I display clean output in Visual studio code with out path or "You may only use..." disclaimer?

I have this simple code in Visual Studio Code
class Program
{
static void Main()
{
Console.WriteLine("Hello!!!");
}
}
All I want to do is get the same result as when I type DOTNET RUN which prints a simple "Hello!!!" to the terminal.
Instead, if i configure it to use the terminal, I get this PATH in blue or if I try to use the debug window, I get this "You may only use..." disclaimer.
How can I set my configuration to either use one of these windows by clicking "Start with out debugging" (not via typing dotnet run manually) to print my output with out any excess info in the red boxes as seen in the images?
If there's a way to configure "dotnet run" in my settings, i'm good with that too so long the output is just "Helloo!!!" on either the debug or terminal window.
You might be able to do this by using an extension like Code Runner which works with Visual Studio Code, it is similar to Script Runner which works with the Atom editor. These are useful for reviewing simple code changes in real-time.
For Python i tried the method below.
In Visual Studio Code, click on the file and then click “RUN” at the top. find “Add Configuration” button on the menu and create a launch.json file by clicking on it. In the file, you will the line ”console”: integratedTerminal . Now change it to “console”:internalConsole. Open your code file and click “RUN” at the top again. Finally if you click on “Run without debugging” button, the output of your code will be display in “Debug Console” at where the terminal is.
Hope it works for you too.

Can't run program because it asks me to attach on process

I'm new to C# Programming, and instead of just letting me run the code by pressing F5, "Attach..." appears where the Run button should be and it won't let me run the code.
This has happened me twice by now and I don't know how to make the "Run" button appear again, because I can't test any program I write.
Make sure you have your solution opened, then right click on your project > Set as Start Up Project
You probably used option recent files which is next to recent projects and solutions option. You still can use recent projects and solutions menu item. There is no bug here.
In my case it was happening because I opened a blank project, when I switched to a Console App Template I could see the run Icon.
Go to %appdata% and delete the Visual Studio folder, this will reset Visual Studio and fix this error.
Go to File > Add > Existing Project and select the one you want to execute.

How does VS compile console applications to show "Press any key to continue"?

When I develop a C# console application (which will run on a server) and I run it using Visual Studio, I get a "Press any key to continue" message before the program terminates.
However, when I compile the very same C# code file manually using CSC, my program doesn't show that message and it terminates immediately after finishing its logic.
Does anyone know how I can make the same feature when compiling the code without using VS and WITHOUT changing the C# code any adding a ReadLine()?
UPDATE : The same message used to appear when I learned C#, I used to use TextPad with CSC, and that message used to appear without adding any Write(Line)/Read(Line) callings
It's nothing to do with the compiler - if you press F5 to debug it rather than Ctrl-F5 to run without debugging, then VS doesn't show the prompt. This is presumably so that you don't miss whatever output it's producing.
In order to do this, Visual Studio runs cmd.exe telling it to run your executable and then pause:
"C:\WINDOWS\system32\cmd.exe" /c ""...\ConsoleApplication1.exe" & pause"
It probably doesn't do it when you debug as it's a bit harder to get the process ID of a child of a child process.
To add a similar option to your program, either use a command line switch to tell the application itself to pause, or use a batch file to run it then pause, or use a shortcut with them cmd.exe /c.
That's not possible. The prompt to press any key is generated by Visual Studio when running a console app. It's not part of your program.
The only way is by using Console.Read() in your code
UPDATE: concerning your remark on using TextPad: I'm not familiar with TextPad, but I wouldn't be surprised if TextPad did the same thing as Visual Studio when running a console app.
You could do this...
static void Main(string[] args)
{
#if DEBUG
Console.Read();
#endif
}
That way the program will not wait for the console input when you build your application as a 'Release'.
You could write a batch script that runs the exe for you and prompts the user to press a key.
The batch script would look something like this:
echo off
YourApp.exe
pause
You could do this, if you want the same functionality when debugging.
if (Debugger.IsAttached)
{
Console.WriteLine("Press any key to continue . . . ");
Console.ReadKey(true);
}
This behavior has nothing to do with the compiler you are using. When you compile with Visual Studio, running the executable outside of Visual Studio actually will perform exactly the same as when you compile with CSC on the command line. Visual Studio (and TextPad) is adding the logic to add the "Press any key to continue" message on the console.
If you want your application to stay open, you will need to do something like Console.ReadLine() to block execution so that your application does not complete its execution.
The question is why would you want to have this behaviour? The Press any key to continue feature is there so that you can see the output of your application. If on the other hand you build your code and run it from a command prompt (console), this will not close when the application finishes, so you can see the output.
As noted above, the Press any key to continue is a feature of the IDE and not related to the code you are writing. The purpose of that feature is to allow you to see the output of you console application.

Categories

Resources