Reading console input in MonoDevelop - c#

I am trying to a simple C# program that takes input and passes it as output. For instance, the output should be:
What is your name?
{user input}
Your name is {user input}
The program is:
public static void Main(string[] args)
{
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);
Console.ReadKey();
}
This is enclosed in a class called 'MainClass'
Its output is:
What is your name?
Your name is:
Why is this not working and how can I make it work?
P.S. I am using MonoDevelop and I added Console.ReadKey(); after the last WriteLine. No change.

You are trying to type in the Application Output window in MonoDevelop and it is read-only.
You can configure MonoDevelop to automatically run the program at the command prompt by right clicking on the "options" menu item of your project and checking Run on external console under the Run > General tree.
alt text http://psf.biz/public/monodevelop_run_on_external_console.jpg
I guess the guy that gave me the -1 was blinded by that huge "Works on My Machine" emblem, nevertheless this is the correct and only answer.

(source: typepad.com)
Is your problem that the program quits immediately after reading the console input? If so, then add a Console.ReadKey(); after the last WriteLine so the program will wait for a keypress. Otherwise, I don't know what the problem is; I copy+pasted the code and it worked.

Related

When using input = Console.ReadLine(); it doesnt go to the next line of code, it simply loops when I put anything

Quick note: I come from python and other easier to use programming languages.
I dont know why it doesnt go and continue to the next line, this is the steps I took, I firstly run the script in visual studio and it runs and then I can input text into the debug console, I type something and it repeats the line of input so I input again and it doesnt go onto the next line it just loops at input = Console.ReadLine();, I even tried copying code from the internet that simply asks for input and prints it and it does the same thing, I think its something to do with visual studio maybe, This is a first so I apologize if I made a mistake, thank you for your help.
here is the code:
using System;
using System.IO;
namespace Printing
{
class Program
{
static void Main(string[] args)
{
string input;
Console.WriteLine("Please enter a number for the times table");
input = Console.ReadLine();
Console.WriteLine("number is: " + input);
for (int i = 0; i < 10; i++)
{
if (i != 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
}
OMG This was very annoying and finally found a way of fixing this, for what ever reason the internal console does not work at all at least on my linux system, I found a post 4 years old mention changing the console to integratedTerminal and that fixed it, it works now, to change the console go to your launch.json file and change internalconsole too integratedTerminal.
very frustrating but now its done.
Something may be wrong with your Visual Studio. I've run your code in my computer and it works perfectly fine, nothing loops. Is VS updated to the latest version? Check Visual Studio Installer. And what console are you using? Is it the default Windows CMD? My advice would be to reinstall VS if it still doesn't work.

Can't change Console.Title on VS2019 C#

Just starting with C# and I'm trying to do very simple things, one of these is changing the console title.
I'm following these instructions: Console.Title Property
The link above is from the Microsoft documentation, and when I copy it in my program it works!
When I try to do the same thing, even simpler... well the title doesn't change at all.
My code:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Hello World Program";
}
}
}
My output:
What am I doing wrong? There is some extra step that I must take which I'm not aware of?
Thank you for your time and help.
After your program exits, Console will no longer have the title you set it to. Use a breakpoint at the end to see what the title is before your program exits.
Console.Title = "New";
return; // Set a breakpoint here.
or you can simply add a 'press any key to continue' (as per the MS docs)
Console.WriteLine("Note that the new console title is \"{0}\"\n" +
" (Press any key to quit.)", Console.Title);
Console.ReadKey(true);

C# Add my Application to Send To When i RightClick on a file

I want to add my Application so when I right click on a file , it shows the Send To > My App.
If its possible then , when I click on the SendTo button , how can I get the Selected file ...??
I didn't tried anything before or even found something that can help.
Thanks :=)
You can add a shortcut to your application to
%AppData%\Microsoft\Windows\SendTo
To navigate to that folder, you can also open a explorer window and type
shell:sendto
into the addressbar.
When you use the Send To context menu, a new instance of your application will be started and you can get the path to the file which you sent to your application via the commandline arguments. For a console application this would be the args parameter of the Main method. Another way is Environment.GetCommandLineArgs();.
Edit: Add sample console application
namespace TestApplication
{
public class Program
{
public void Main(string[] args)
{
String filePath = args[0];
Console.Write("The file you sent here: ");
Console.WriteLine(filePath);
Console.ReadLine();
}
}
}
This assumes that the app ist started with no other arguments. If there are other arguments, the filepath could be on another index in the args array.
Sample output of this console app could be:
The file you sent here: C:\tmp\file.txt

Passing args from a batch file to a C# program with a space

I'm trying to pass arguments/parameters from a batch file to a simple C# Console.Writeline(args[0]) program.
My program's exe file has a two-word name with a space and that's the problem! The whole thing only works when I rename the exe file to a whole word and write start SchoolBook.exe OMG and the arg[0] then becomes equal to "OMG".
But in any other case the program won't start.
If I leave it start School Book.exe OMG it says "Windows cannot find "School".. "
If I put double quotes: start "School Book.exe" OMG it says "Windows cannot find "OMG".. "
If I put single quotes it can't find "School"
I tried putting a '-' before the OMG, putting quotes around the OMG, even putting %1% before it NO! Nothing works..
Any help?
Thank you in advance!
I just created an empty command application "School Book" which when built produces the executable School Book.exe.
My pretty empty implementation:
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
MessageBox.Show(args[0]);
}
else
{
MessageBox.Show(args[0]);
}
}
}
In my command box I can execute the program like this:
Debug>start "" "School Book.exe" OMG
Now a messagebox pops up with "OMG"

Seeing the console's output in Visual Studio 2010?

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 output window.
The "program output" tag is already checked, and I already redirected all outputs to the intermediate window but to no avail.
How do I enable seeing the program's output?
I don't think the problem lies with my code. I tried running a simple program that just outputs a string and readline "ala hello world" and I am still unable to see any output. The problem is either with me looking for the output in the wrong location or Visual Studio acting out.
The debug.write method also doesn't work.
Using debug.Write, it all works, though it didn't before. Either something bugged out with me before I restarted or I just need to take a break, either way it's all good now. Thanks all for the helpful comments =)
You can use the System.Diagnostics.Debug.Write or System.Runtime.InteropServices method to write messages to the Output Window.
Here are a couple of things to check:
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).
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)
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)
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.
Press Ctrl + F5 to run the program instead of F5.
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.
To keep open your windows console and to not use other output methods rather than the standard output stream cout go to Name-of-your-project -> Properties -> Linker -> System.
Once there, select the SubSytem Tab and mark Console (/SUBSYSTEM:CONSOLE). Once you have done this, whenever you want to compile use Ctrl + F5 (Start without debugging) and your console will keep opened. :)
I run into this frequently for some reason, and I can't fathom why this solution hasn't been mentioned:
Click View → Output (or just hold Ctrl and hit W > O)
Console output then appears where your Error List, Locals, and Watch windows are.
Note: I'm using Visual Studio 2015.
Visual Studio is by itself covering the console window, try minimizing Visual Studio window they are drawn over each other.
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
You could create 2 small methods, one that can be called at the beginning of the program, the other at the end. You could also use Console.Read(), so that the program doesn't close after the last write line.
This way you can determine when your functionality gets executed and also when the program exists.
startProgram()
{
Console.WriteLine("-------Program starts--------");
Console.Read();
}
endProgram()
{
Console.WriteLine("-------Program Ends--------");
Console.Read();
}

Categories