[Mono-osx] Howto detect os?

Heertsch heertsch at gmail.com
Mon Sep 14 13:30:27 EDT 2009



Michael Hutchinson wrote:
> 
> On Sun, Sep 13, 2009 at 3:45 PM, Heertsch <> wrote:
>> Hi
>> is there a way to detect, if the app runs under linux or osx?
> 
> In MD we use some detection code factored out of
> Managed.Windows.Forms/XplatUI:
> 
> http://anonsvn.mono-project.com/viewvc/trunk/monodevelop/main/src/core/Mono.Texteditor/Mono.TextEditor/Platform.cs?view=markup
> -- 
> Michael Hutchinson
> I removed the GTK# reference and did some cosmetics (your code won't start
> on my VisualStudio) (see code below).
It works fine in Visual Studio and with MD on OS X.
But if I try to start it in MD with OpenSuse I get: (Back translation from
German)
Error loading project foo.csproj: Could not set property 'Policies' in type
'DotNetProject' 
No idea, what that means. (Probably it does not depend on the code below.)
Andreas

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Basal.Code{
    public static class Platform{
        [DllImport("libc")]
        static extern int uname(IntPtr buf);
        static private bool mIsWindows;
        static private bool mIsMac;
        static private bool mIsX11;
        public enum OS {Windows, Mac, X11,unknown};
        static public OS GetOS(){
            if(mIsWindows=(System.IO.Path.DirectorySeparatorChar ==
'\\'))return OS.Windows;
            if(mIsMac=(!mIsWindows && IsRunningOnMac()))return OS.Mac;
            if(!mIsMac && System.Environment.OSVersion.Platform ==
PlatformID.Unix)return OS.X11;
            return OS.unknown;
        }
        //From Managed.Windows.Forms/XplatUI
        static bool IsRunningOnMac(){
            IntPtr buf = IntPtr.Zero;
            try{
                buf = Marshal.AllocHGlobal(8192);
                // This is a hacktastic way of getting sysname from uname ()
                if (uname(buf) == 0){
                    string os = Marshal.PtrToStringAnsi(buf);
                    if (os == "Darwin")return true;
                }
            }
            catch { }
            finally{
                if (buf != IntPtr.Zero) Marshal.FreeHGlobal(buf);
            }
            return false;
        }
    }
}
-- 
View this message in context: http://www.nabble.com/Howto-detect-os--tp25426473p25439993.html
Sent from the Mono - OSX mailing list archive at Nabble.com.



More information about the Mono-osx mailing list