How to use Console.ReadLine() in a project using MonoDevelop under Linux - c#

I have the following sample project and I use MonoDevelop in OpenSuse.
I can build the project, but when I execute it, it does not show any console where I can enter the string. I am aware about people indicating that we should set the Project options to "Run on External Console". That option is enabled, but there is no console appearing when I execute the program under MonoDevelop. Do we have to open a console separately and attach to process, or is there another trick?
using System;
namespace ConsoleReadLine
{
class MainClass
{
public static void Main (string[] args)
{
string s = Console.ReadLine ();
Console.WriteLine (s);
Console.ReadLine ();
}
}
}

Currently (Version 7.8.4 (build 2)), this is still an issue on Linux.
To address it, you need to:
Right-click on the solution (and not the project), and select "Options"
In "Run", select "Default" under "Configurations", tick "Run on external console"
Click "OK", and that's it!
I don't know if there is any way to change this behavior system-wide (that is, for all solutions), but would be interested in knowing.

Related

Unable to run C# in Visual Studio Code

Whenever I attempt to run C# in VS Code, the program asks for the environment. When I pick ".NET 5+ and .NET core" all that happens is a settings.json file gets opened up as another tab. The second environment option is ".NET Framework 4.x (Windows Only)" all that happens then is that a flash of the code running bar pops up.
However it can run Java, but I do not want to do Java. It could be an user error, I wouldn't be surprised if I just couldn't find how to run code.
The problem is that the code runs and exits immediately
You should add Console.ReadKey() at the end of the Main method like this:
using System;
class Program
{
static void Main(string[] args)
{
// Your Code here
Console.ReadKey(); // At the end of the Main method
}
}

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.

C# Console Application build but not launch application

I have a simple Console Application write with C-Sharp language (Visual Studio 2013):
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When I press F5 or click Start button, my project was built, but not launch.
Sometime, Ouput windows says:
Error 12 Could not copy "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". Exceeded retry count of 10. Failed.
Error 13 Unable to copy file "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". The process cannot access the file 'bin\Debug\HelloWorld.exe' because it is being used by another process.
but when I write Windows Form Application, my project was built and launch normally ???
Why? and How to solve this problem ?
This is most likely due to windows keeping the process open. Your only option is to try and kill all processes of your app in task manager->processes.
The next thing to try is to simply change the build from debug to release, this should build another executable in the release folder as opposed to debug. By no means is this a silver bullet but, hopefully a sufficient workaround.
Before you try the release build, attempt to try and fix the problem. I've seen windows moan at just having my debug folder open and my exe selected because I suspect the thumbnail was being displayed thus being "used" in windows etc.
I found reason and solve for my problem.
I tried to restart Application Experience Service and the problem was solved.
Please restart yor Visual Studio and then try again. It will work and delete your bin/debug folder again.
I have faced with this issue.
That happeneds when an instance of software is running whether by visual studio or by your self
solution :
you should find your application name in the processes of task manager
your's is :
HelloWorld.exe
select that and click on the end task button
now you can start debugging and running you'r app.
at least worked for me.

Visual Studio clears output file at end of debugging

I am experiencing a weird behaviour from Visual Studio 2013. I've got a C# program which writes to the standard output, let's say
using System;
using System.Threading;
namespace CsSandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
Thread.Sleep(10000);
}
}
}
In the Debug tab of the project's properties I have redirected the output to a file, like this:
If I open the file within those 10s when my application is still running, the file does contain "Hello world!". However, as soon as the program exits, the file is cleared. This does not happen when I run the program from the command line.
Is there a rationale why Visual Studio does it? Is there a way to bypass this behaviour?
I believe this is due to the way Visual Studio hosts your application, in order to reduce startup time when debugging.
What happens is that your application (Program.exe) will actually be hosted in another process (Program.vshost.exe), which is started with the same command line arguments. When your application ends, this process is immediately restarted. You should be able to see this within Task Manager - look in the details tab so you can see the PID of the processes, and run your app - you'll see one Program.vshost.exe instance which ends when your app finishes, and another one come up immediately. That's then overwriting your output file.
One fix for this is to give your application a command line argument for the file to write to - and open that file within your Main method. You won't get there until you start running the app again.
Another option would be to simply stop using the hosting process - in the Debug part of your project properties, at the bottom you should see a checkbox for "Enable the Visual Studio hosting process". Uncheck this and I think your problem will go away - but it'll take longer to start debugging.
See this blog post for more information about the hosting process.

Compiling C# Code in Visual C# 2010 Express

I just have one question regarding C#.
I have downloaded Microsoft Visual C# 2010 Express to write the C# code.
Now, I want to compile the code using the same visual C#. Can I?
I have searched for a method to compile the code but all of the methods I founded are talking about command line 'cmd'.
I tried using it but it gives me that "csc is not recognized as an internal or external command ....." although that the directory I was in is the same as the C# code directory
I want to compile and see the results in the output.
This is the original code to compile:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project1
{
class Example1_1
{
public static void Main()
{
// display “Hello World!” on the screen
System.Console.WriteLine("Hello World!");
// display the current date and time
System.Console.WriteLine("The current date and time is " +
System.DateTime.Now);
}
}
}
and the result of pressing F6 'build' is:
------ Build started: Project: Project1, Configuration: Release x86 ------
Project1 -> C:\Users\Ali Alnufaili\AppData\Local\Temporary Projects\Project1\bin\Release\Project1.exe
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
If you have any suggestion, till me please. ^_^
Just press this button:
or hit F5 to compile and run your code.
EDIT:
So you are running a console application and write some text to the console. Maybe your problem is that the console window pops up and closes immediately?
Try adding System.Console.ReadKey(); at the bottom of your Main method. Then the console window will stay open until you hit a key.
Or go the directory where your compiled program is (looks like it is C:\Users\Ali Alnufaili\AppData\Local\Temporary Projects\Project1\bin\Release), open a command prompt (in windows explorer, hold down SHIFT and press the right mouse button and choose Open command prompt here), and run the executable in the command prompt (just type Project1.exe and hit enter)
If you've created a project in Visual Studio, you should be able to simply use the "build" option from the menus/toolbars.
If you've got the default C# key bindings, I believe F6 is a shortcut to start under the debugger.
I'm sure you have used some tutorial for the example. Did the tutorial not mention pressing F5 to compile and run the application?
Your Build output says your program has compiled successfully, but to start your application, too, you need to press F5.
Also, currently your program will immediately exit when done. To make it wait, add the following line at the end:
Console.ReadLine();
That will let you see your output and then the program will wait until you press enter.

Categories

Resources