using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PlatformDetector_CS
{
    public enum Platform
    {
        PocketPC, WindowsCE, Smartphone, Win32NT, Unknown
    }

    class PlatformDetector
    {
        [DllImport("coredll.dll")]
        private static extern bool SystemParametersInfo(
            int uiAction,
            int uiParam,
            StringBuilder pvParam,
            int fWinIni);

        private static int SPI_GETPLATFORMTYPE = 257;

        public static Platform GetPlatform()
        {
            Platform plat = Platform.Unknown;
            switch (System.Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                    plat = Platform.Win32NT;
                    break;
                case PlatformID.WinCE:
                    plat = CheckWinCEPlatform();
                    break;
            }

            return plat;
        }

        static Platform CheckWinCEPlatform()
        {
            Platform plat = Platform.WindowsCE;
            StringBuilder strbuild = new StringBuilder(200);
            SystemParametersInfo(SPI_GETPLATFORMTYPE, 200, strbuild, 0);
            string str = strbuild.ToString();
            switch (str)
            {
                case "PocketPC":
                    plat = Platform.PocketPC;
                    break;
                case "SmartPhone":
                    // Note that the strbuild parameter from the
                    // PInvoke returns "SmartPhone" with an
                    // upper case P. The correct casing is
                    // "Smartphone" with a lower case p.
                    plat = Platform.Smartphone;
                    break;
            }
            return plat;
        }
    }
}

