3

I'm using MonoDevelop to write C# (.NET) codes, on Ubuntu Desktop 14.0.5 LTS. While testing a simple Hello World code, I encountered a problem - earlier on windows, I used the Console.ReadKey() statement to make sure that the console doesn't disappear instantaneously after showing the output. However, this trick is not working on Ubuntu. Here's the code, in case you'd need to see it:

using System;

namespace Project_1 {
    class MainClass {
        public static void Main (string[] args) {
            Console.WriteLine ("Hello World!");
            Console.ReadKey ();
        }
    }
}

In spite of the last statement, the console shows up for about 1/100th of a second before disappearing. Precisely, the Console.ReadKey() statement is being ignored(??). I have no idea why this is happening. I'm new to using Ubuntu and installed MonoDevelop a few hours ago.

How do I fix this issue?

  • 1
    It's probably because you're not running the application from the terminal, so there is no standard input. Try running it from the terminal. – Chai T. Rex Apr 16 '17 at 18:12
  • Before you try running the application from the terminal, please read the installation instructions in my answer to make sure that you have installed the necessary package (mono-complete). – karel Apr 17 '17 at 10:34
2

You need to install mono-complete instead of Mono development tools (mono-devel) package if you want to run software for Mono or Microsoft .NET which you are not installing from a Debian package.


  1. Install mono-complete

    sudo apt-get install mono-complete
    
  2. Save your C# code in a file called hello.cs.

  3. Make hello.cs executable. Right-click the hello.cs file -> select Properties -> Permissions tab -> put a check mark to the left of Allow executing file as program.

  4. Change directories using the cd command to the directory that contains the hello.cs file.

  5. Use the mcs compiler and create a Windows executable named hello.exe from the source hello.cs.

    mcs -out:hello.exe hello.cs
    
  6. Run the hello.exe program with mono.

    mono hello.exe
    
  7. The results of running your program in step 6. should be:

    Hello World!  
    
  8. Press Enter to exit back to a default terminal prompt.

Run C# program in MonoDevelop

  1. Install MonoDevelop

    sudo apt-get install monodevelop # 17.10 and earlier
    
  2. Open MonoDevelop application.

  3. Create a new project. Select File -> New -> Solution -> .NET -> Console Project -> check the dropdown menu next to Console Project to make sure that C# is selected -> click the Next button -> select a name and directory location for your project -> click the Create button.

  4. In the left pane select Program.cs. Copy your C# code into Program.cs.

  5. Select Build -> Build All.

  6. Click the Run arrow in the upper left corner of MonoDevelop to run the program.

  • Sorry for the late response. I had forgotten that Ubuntu does not run .exe files, which CS codes produce, which could be causing the issue. And Using was a typo. However, I'm having another issue: I installed mono-complete but I think MonoDevelop is not recognizing that because I'm still the facing the same problem. Is there a way to make MD recognize the newly installed package without reinstalling the software? – progyammer Apr 17 '17 at 18:04
  • 1. The easiest way to run an .exe file (e.g. file-name.exe) is from the terminal with mono file-name.exe 2. The easy way to get MonoDevelop to recognize mono-complete is to uninstall MonoDevelop and remove its config files using the command sudo apt-get purge monodevelop and then reinstall MonoDevelop. – karel Apr 17 '17 at 18:11
  • I could perfectly run it from the terminal. I'll now reinstall MD and if I have no further issues, I'll mark this answer. Thank you for your help. – progyammer Apr 17 '17 at 18:15
  • 1
    If MonoDevelop doesn't work after you reinstall it you'll have to add the path to mono runtime manually in MonoDevelop -> Edit -> Preferences -> Projects -> .NET Runtimes -> click the Add button -> select the mono installation (/usr/bin/mono). After that set the new Mono Runtime as default by clicking the Set as Default button. – karel Apr 17 '17 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.