5

What command should I use to start this C# program from the command line in Linux? I've already compiled it (using Monodevelop), but I don't know how to start it from the command line.

using System;
class ExampleClass
{
    static void Main()
    {
        Console.WriteLine("Hello, world!");
    }
}
6
  • 1
    Did you already compile it? Do you have a binary in the path or in the current directory? Commented Oct 21, 2012 at 1:54
  • 5
    If it was compiled you would do mono myprogram.exe Commented Oct 21, 2012 at 1:56
  • 1
    Also, why is this question being downvoted? I know this is an obvious question (for non-beginners) - is that bad for some reason? Commented Oct 21, 2012 at 2:03
  • @MarkHall Does this apply to Windows as well as Linux? Commented Oct 21, 2012 at 2:25
  • 2
    There is a meta discussion about this question. Commented Oct 21, 2012 at 2:29

1 Answer 1

9

The command line you need to start a C# (or any other .NET) program on Linux depends on how you have your Linux system configured.

The standard answer is to run the mono program, and pass the name of your executable assembly as a parameter. The name of your executable assembly is typically the same as the name of your project file, though you can easily change it; just look for a file ending in .exe after you're done compiling. It will be found in a folder named bin\Debug, or bin\Release or something similar (it depends on how you have your project build settings set up). So, if you built a program called hello.exe you would go into your project folder and run:

~/projects/hello $ mono bin\Debug\hello.exe

The reason you need to run the mono program is because Linux does not know, by default, how to run the .NET runtime automatically. When you install .NET on Windows, it actually changes the part of the OS that loads programs, so Windows just automatically recognizes a .NET program and loads the runtime. On Linux, you need to do that yourself, by running the mono program first.

If you run a lot of managed code on Linux, you can also configure the Linux kernel to work the same way that Windows does. Linux has support for "miscellaneous binary formats" that allows you to tell Linux how to execute binaries that are not native Linux format. This is somewhat advanced - it likely requires you to build a custom kernel, though I would not be surprised if your Linux distribution had a better way to do it. More information on this process can be found here:

http://www.kernel.org/doc/Documentation/mono.txt

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.