[Mono-list] Detect OS using mono framework

Scott Graves scott.e.graves at gmail.com
Thu Mar 27 11:18:16 EDT 2008


I came up with a temporary solution for now similar to what the site
descibes:
public class OSDetection
    {
        static public bool IsWindows
        {
            get
            {
                return Environment.OSVersion.Platform != PlatformID.Unix;
            }
        }

        static public bool IsOSX
        {
            get
            {
                bool retVal = false;
                if (!IsWindows)
                {
                    ProcessStartInfo si = new ProcessStartInfo("uname",
"-s");
                    si.RedirectStandardOutput = true;
                    si.UseShellExecute = false;

                    string outputString = string.Empty;
                    using (Process p = Process.Start(si))
                    {
                        p.Start();
                        outputString = p.StandardOutput.ReadLine();
                        p.WaitForExit();
                    }

                    retVal = string.Compare(outputString, "darwin", true) ==
0;
                }

                return retVal;
            }
        }

        static public bool IsLinux
        {
            get
            {
                bool retVal = false;
                if (!IsWindows)
                {
                    ProcessStartInfo si = new ProcessStartInfo("uname",
"-s");
                    si.RedirectStandardOutput = true;
                    si.UseShellExecute = false;

                    string outputString = string.Empty;
                    using (Process p = Process.Start(si))
                    {
                        p.Start();
                        outputString = p.StandardOutput.ReadLine();
                        p.WaitForExit();
                    }

                    retVal = string.Compare(outputString, "linux", true) ==
0;
                }

                return retVal;
            }
        }
    }

On Wed, Mar 26, 2008 at 11:06 PM, Oleg Deribas <
thisaddressisnotmine at td.selfip.net> wrote:

> Hi,
>
> Scott Graves wrote:
>
> > I now have a requirement to execute a method based on the targeted OS.
> > As of today, the code is 100% portable across OS X, Linux, and Wn32.
> > Using the mono framework, is there a way to determine this by using a
> > Conditional attribute or any other means?
>
> Partially it is described here:
>
> http://mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
>
> --
> Oleg
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080327/9c645d5e/attachment.html 


More information about the Mono-list mailing list