Command Line Execution

C# shields you from the differences between operating systems with its File, Path, and Directory classes.

If you leave Xamarin Studio and go to the command line as described in Command Line Introduction, then you are exposed to the differences between the operating systems. Look over that section.

Thus far we have let Xamarin Studio hide what actually is happening when you execute a program. The natural environment for the text-based programs that we are writing is the command line. We need to get outside of Xamarin Studio.

  1. To show off the transition, first run the addition1 example project from inside Xamarin Studio.

  2. Open a terminal on a Mac or a Mono Command Prompt console in Windows.

  3. Following the Command Line Introduction, change the current directory to the addition1 example project directory.

  4. Enter the command to list the directory (dir in Windows; ls on a Mac).

  5. You should see addition1.cs but not see addition1.exe. The file addition1.exe is the compiled program that Xamarin Studio creates for this project, however, with the default configuration that we kept for this project, this file ends up two directories down in bin/Debug.

Let us now create addition1.exe in the main project directory without using Xamarin Studio. Continue with the same terminal/console window:

  1. Try the command

    mono addition1.exe
    

    You should get an error message, because addition1.exe is not in the current directory.

  2. Enter the command

    mcs addition1.cs
    

    This is the Mono system compiler, building from the source code.

  3. Print a listing of the directory. You should now see addition1.exe, created by the compiler.

  4. Try the command again:

    mono addition1.exe
    

    Note that no new terminal window popped up and later disappeared - output appears in and stays in the current terminal window.

  5. Windows only: On Windows, Xamarin Studio creates a regular Windows executable file. For consistency you can use the command above, but you no longer need Mono. You can just enter the command addition1.exe or the shorter addition1.

Now try a program that had multiple files. The project version addition3 uses the library class UIF. Continue with the same terminal/console window:

  1. Enter the commands:

    cd ../addition3
    mcs addition3.cs
    

    to get to the addition3 project folder, and attempt to compile its program. You should get an error about missing the UIF class. The mcs program does not know about the information Xamarin Studio keeps in its references.

  2. Extend the command to also give the location of the library file:

    mcs addition3.cs ../ui/uif.cs
    

    That should work, now referring to both needed files.

  3. Enter the command

    mono addition3.exe
    
  4. Now let us try a project where we read a file. Enter command

    cd ../sum_file
    
  5. List the contents of this directory. (dir on Windows; ls on a mac).

  6. If you have run the sum_file.cs program before, you should see sum_file.exe listed, since the Xamarin options for this project were set to place the output in this main project directory. Erase sum_file.exe with erase sum_file.exe on Windows or rm sum_file.exe on a Mac. You can list the directory again to check that you did it.

  7. Now enter the command

    mcs sum_file.cs
    
  8. List the directory again - sum_file.exe has been created again.

  9. Now enter the command

    mono sum_file.exe
    

    As the program runs, remember the file numbers.txt is in the same folder. To use it, just enter the simple file name, numbers.txt.

  10. For a little more command-line experience enter type numbers.txt on Windows, or cat numbers.txt on a Mac. You should see that the the numbers in the file do add to the program’s result: 16.

By default mcs and mono read from and write to the current directory of the terminal/console. This is unlike the Xamarin Studio default, where the current directory for execution is not the project directory. Under the hood, Xamarin Studio uses mcs also, with a bunch of further options in the parameters, changing the execution directory and also arranging for better debugging information when you get a runtime error.

Xamarin Studio keeps track of all of the parts of your projects, and recompiles only as needed. There are also many command-line tools that manage multi-file projects neatly, remembering the parts, and compiling only as necessary. One example is NAnt, which comes with Mono.