C# 如何检测用户控件是否在IDE、调试模式或已发布的EXE中运行?

C# 如何检测用户控件是否在IDE、调试模式或已发布的EXE中运行?,c#,ide,user-controls,C#,Ide,User Controls,我正在构建一个用户控件。它的目的是向用户显示类的状态。显然,这并不重要,当控件在IDE中运行时,它会减慢速度,就像您将它添加到表单时一样 解决此问题的一种方法是在运行时创建控件并将其添加到表单的控件集合中。但这似乎不够完美 有没有办法在控件中设置一个标志,这样它就可以根据运行方式跳过某些代码段 p、 我正在使用C#和VS2008,我在另一篇文章中找到了这个答案,它似乎更简单,对我的情况来说效果更好 public static bool IsInRuntimeMode( IComponent co

我正在构建一个用户控件。它的目的是向用户显示类的状态。显然,这并不重要,当控件在IDE中运行时,它会减慢速度,就像您将它添加到表单时一样

解决此问题的一种方法是在运行时创建控件并将其添加到表单的控件集合中。但这似乎不够完美

有没有办法在控件中设置一个标志,这样它就可以根据运行方式跳过某些代码段


p、 我正在使用C#和VS2008,我在另一篇文章中找到了这个答案,它似乎更简单,对我的情况来说效果更好

public static bool IsInRuntimeMode( IComponent component ) {
    bool ret = IsInDesignMode( component );
    return !ret;
}

public static bool IsInDesignMode( IComponent component ) {
    bool ret = false;
    if ( null != component ) {
        ISite site = component.Site;
        if ( null != site ) {
            ret = site.DesignMode;
        }
        else if ( component is System.Windows.Forms.Control ) {
            IComponent parent = ( (System.Windows.Forms.Control)component ).Parent;
            ret = IsInDesignMode( parent );
        }
    }

    return ret;
}


这是我在项目中使用的方法:

//use a Property or Field for keeping the info to avoid runtime computation
public static bool NotInDesignMode { get; } = IsNotInDesignMode();
private static bool IsNotInDesignMode()
{
    /*
    File.WriteAllLines(@"D:\1.log", new[]
    {
        LicenseManager.UsageMode.ToString(), //not always reliable, e.g. WPF app in Blend this will return RunTime
        Process.GetCurrentProcess().ProcessName, //filename without extension
        Process.GetCurrentProcess().MainModule.FileName, //full path
        Process.GetCurrentProcess().MainModule.ModuleName, //filename
        Assembly.GetEntryAssembly()?.Location, //null for WinForms app in VS IDE
        Assembly.GetEntryAssembly()?.ToString(), //null for WinForms app in VS IDE
        Assembly.GetExecutingAssembly().Location, //always return your project's output assembly info
        Assembly.GetExecutingAssembly().ToString(), //always return your project's output assembly info
    });
    //*/

    //LicenseManager.UsageMode will return RunTime if LicenseManager.context is not present.
    //So you can not return true by judging it's value is RunTime.
    if (LicenseUsageMode.Designtime == LicenseManager.UsageMode) return false;
    var procName = Process.GetCurrentProcess().ProcessName.ToLower();
    return "devenv" != procName //WinForms app in VS IDE
        && "xdesproc" != procName //WPF app in VS IDE/Blend
        && "blend" != procName //WinForms app in Blend
        //other IDE's process name if you detected by log from above
        ;
}

注意!!!:返回的代码bool在设计模式下指示不是

太棒了!谢谢这比我想象的要容易得多!有没有想过为什么我的页面对象上的站点属性总是空的?因为webforms designer不支持这种情况。我不知道为什么:(.此解决方案在组件(非UI组件)中工作)designer和winforms designer。它应该也适用于非web第三方设计器。当与UserControl一起使用时,我也会得到一个空站点对象。是否支持UserControl?@BenGripka是的,支持UserControl。但是,此示例仅适用于winforms。对于ASP.NET或WPF,必须使用其他方法。
//use a Property or Field for keeping the info to avoid runtime computation
public static bool NotInDesignMode { get; } = IsNotInDesignMode();
private static bool IsNotInDesignMode()
{
    /*
    File.WriteAllLines(@"D:\1.log", new[]
    {
        LicenseManager.UsageMode.ToString(), //not always reliable, e.g. WPF app in Blend this will return RunTime
        Process.GetCurrentProcess().ProcessName, //filename without extension
        Process.GetCurrentProcess().MainModule.FileName, //full path
        Process.GetCurrentProcess().MainModule.ModuleName, //filename
        Assembly.GetEntryAssembly()?.Location, //null for WinForms app in VS IDE
        Assembly.GetEntryAssembly()?.ToString(), //null for WinForms app in VS IDE
        Assembly.GetExecutingAssembly().Location, //always return your project's output assembly info
        Assembly.GetExecutingAssembly().ToString(), //always return your project's output assembly info
    });
    //*/

    //LicenseManager.UsageMode will return RunTime if LicenseManager.context is not present.
    //So you can not return true by judging it's value is RunTime.
    if (LicenseUsageMode.Designtime == LicenseManager.UsageMode) return false;
    var procName = Process.GetCurrentProcess().ProcessName.ToLower();
    return "devenv" != procName //WinForms app in VS IDE
        && "xdesproc" != procName //WPF app in VS IDE/Blend
        && "blend" != procName //WinForms app in Blend
        //other IDE's process name if you detected by log from above
        ;
}