.NET Compact Framework - 检测是否在仿真器下?

问题描述:

有没有一种方法来检测我们是否在.NET CF代码上运行在模拟器或实际设备上?.NET Compact Framework - 检测是否在仿真器下?

感谢 多米尼克

This article告诉你如何,间接的影响。它展示了如何创建一个实用的方法IsEmulator。如果您一般关心平台检测,您可能也对follow-up感兴趣。

从文章:

using System; 
using System.IO; 
using System.Windows.Forms; 
using Microsoft.Win32; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace PlatformDetection 
{ 
    internal partial class PInvoke 
    { 
     [DllImport("Coredll.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode)] 
     static extern int SystemParametersInfo4Strings(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni); 

     public enum SystemParametersInfoActions : uint 
     { 
      SPI_GETPLATFORMTYPE = 257, // this is used elsewhere for Smartphone/PocketPC detection 
      SPI_GETOEMINFO = 258, 
     } 

     public static string GetOemInfo() 
     { 
      StringBuilder oemInfo = new StringBuilder(50); 
      if (SystemParametersInfo4Strings((uint)SystemParametersInfoActions.SPI_GETOEMINFO, 
       (uint)oemInfo.Capacity, oemInfo, 0) == 0) 
       throw new Exception("Error getting OEM info."); 
      return oemInfo.ToString(); 
     } 

    } 
    internal partial class PlatformDetection 
    { 
     private const string MicrosoftEmulatorOemValue = "Microsoft DeviceEmulator"; 
     public static bool IsEmulator() 
     { 
      return PInvoke.GetOemInfo() == MicrosoftEmulatorOemValue; 
     } 
    } 
    class EmulatorProgram 
    { 
     static void Main(string[] args) 
     { 
      MessageBox.Show("Emulator: " + (PlatformDetection.IsEmulator() ? "Yes" : "No")); 
     } 
    } 
} 

如果您使用的OpenNETCF Smart Device Framework,可以测试OpenNETCF.WindowsCE.DeviceManagement.OemInfo属性,看它是否等于“微软DeviceEmulator”。这就是我如何检测到我在仿真器下运行,不应该与特定的硬件(如条形码阅读器)进行交互。