Executing C# code in Linux


The .NET centric applications are meant to windows operating system up till now, but now Microsoft has introduced a new cross-platform application called Mono which enables the execution of the application developed under the .NET platform in Linux environment by giving an impression in such a way that as if we are running Linux package rather than executing .exe file.

Mono

Mono is an open-source utility that allows the developer to execute .NET centric applications on other platforms such as Mac or Linux as it provides an installation package for Windows platform to compile and execute .NET assemblies on Windows OS without ever installing the Visual Studio IDE or .NET Framework SDK. Hence, we can build real-time, production-ready assemblies that use Windows Forms, LINQ, XML web services, ADO.NET and ASP.NET by taking advantage of existing core CLR namespaces under Mono. First, download the Mono binaries using the wget utility and execute these series of commands to configure it properly as;

wget --no-check-certificate https://raw.github.com/nathanb/iws- snippets/master/mono-install-scripts/ubuntu/install_mono-3.0.sh
chmod 755 install_mono-3.0.sh
./install_mono-3.0.sh

Apart from that, install the MCS package too alternatively, to compile the .NET binary as following;

root/kali:~/ sudo apt-get install mcs

C# Code Compilation

The infrastructure of the Mono console application is almost similar to the traditional C#.NET console application. To develop the first Mono based console application (test.cs), open any code editor like VIM and type the following code.

using System;
namespace test {
   class test{
      public static void Main(string[] args) {
         System.Console.WriteLine("C# app Compiled on Kali Linux");
      }
   }      
}

Then, open the Terminal and hit the following commands to compile the code.

root/kali:~/ mcs test.cs
root/kali:~/ ls
test.cs test.exe

The aforesaid command will generate an executable file like windows. Now hit the ./test.exe or mono test.exe command to run the C# binary; Here, the screenshot summarized everything we have done so far.

Updated on: 05-Jan-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements