Posts Tagged ‘mono’

.NET MONO Which Operating System is Running?

Monday, June 28th, 2010

Do you want to execute operating system specific code (exes) in the .NET/Mono framework? I’ll go over how you can do that.

Below is part of a class I had written that can determine if Linux or Windows is running. Keep in mind that for the code to properly detect Mac OS, you will need to figure out the proper platform number and modify the code respectively.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace namespace1
{
 static class HostOS
 {
  /// <summary>
  /// an enumerator that holds a list of possible operating systems
  /// this code can execute on
  /// </summary>
  public enum HostEnviroment
  {
   Windows,
   Linux
  }

  /// <summary>
  /// determine the operating system this program is running on
  /// </summary>
  /// <returns>the current operating system</returns>
  /// <remarks>
  /// the current choices are only linux or windows
  /// it really only checks to see if the OS is linux,
  /// otherwise, it defaults to windows
  /// if other OS are inplemented in the future, this
  /// would need to be changed
  /// </remarks>
  public static HostEnviroment determineHostEnviroment()
  {
   HostEnviroment currentHost;

   int platformNumber = (int)Environment.OSVersion.Platform;

   //determine if the host is a *nix operating system
   //these numbers take into account historic values,
   //do an internet search for details
   if ((platformNumber == 4) || (platformNumber == 6)
   || (platformNumber == 128))
   {
    //running on *nix
    currentHost = HostEnviroment.Linux;
   }
   else
   {
    //not running on *nix (this function defaults to windows)
    currentHost = HostEnviroment.Windows;
   }

   return currentHost;
  }

  //............
 }
}

I had written this class because I have been writing code that I develop on Windows, but needs to run in Linux. Ideally, I wanted it to work in both operating systems for various reasons. The program I was writing needed to call a few console applications that were operating system specific, but had versions for both Windows and Linux. Thanks to the System.Diagnostics namespace, it’s quite easy to run the OS specific code!

.NET and MONO

Sunday, April 19th, 2009

I’ve been working on a personal project with a friend for quite a while now.  To make a long story short it deals with loading/editing/saving large quantities of public geographic data. Lately we have been throwing around the idea of having our database server’s OS in Linux instead of Windows 2000.

Which brings me to the MONO project. I’ve started testing some code I wrote in C# (a console application that accesses the Internet) by using that MS Virtual PC program I setup in my previous posting with Xubuntu. While my current goal changed a bit, I’m glad to start getting some use out of it. Basically, all I needed to do was install the mono run-time libraries and I was good to go. Now keep in mind that some Linux distributions might only have older versions of MONO available by way of a package manager (or pre-install). In this case you should see if your .NET application requires something that the available version of MONO does not have by visiting the mono project website. Otherwise you need install MONO from source (search google for a tutorial, I’ve seen at least one).

I have had one issue so far in how I wrote my application. File paths are of course difference between Windows and Linux. The issue that I’m looking into has to deal with the path separators \ and /. I currently have the program hard coded to take a path specified and append a \ between that and a filename. In linux this doesn’t work properly, so the path and filename are a bit screwed up. More specifically if I specify /home/username/Desktop, the program will save into /home/username with a filename of /Desktop-filename.ext… I’ll update this or make a new posting once I figure out how to fix that.

EDIT: This page on the mono site details about path separators.
“To write portable software, you must make sure that you use the System.IO.Path.DirectorySeparatorChar (System.IO.Path.DirectorySeparatorChar) character when you must concatenate paths, or even better, use the System.IO.Path.Combine (System.IO.Path.Combine(string,string)) method to combine pathnames.”

All that being said, I noticed on go-mono.com that they offer a VMWare file that has the latest version of MONO pre-installed on OpenSUSE Linux. I’m currently in the process of downloading the ISO and the VMWare Player and will see if it is any better than Virtual PC with Xubuntu that I’m currently using.

2nd EDIT: Started trying out VMWare and the MONO SUSE Linux image. It’s much much better than Using the MS VIrtual PC program. So I suggest you go that route if you are interested in trying something similar.

3rd EDIT: Sun VirtualBox looks like a good program to use too. It’s freely available and seems to have more features than the free VMWare does.

I’m impressed with MONO so far!




The Way Of Coding



 
Scott J. Waldron Photography
Stock Photo Website
Tech Learning Site
Follow me on Twitter

Popular Article Tags

Archives

Pages