HOWTO: Detect design mode in WPF, ASP .NET and Windows Forms applications

If you want to detect the design mode in code, I’m using the following method, that is working fine for WPF, ASP .NET and Windows Forms applications. It’s easy, stable, bug free and has a very good performance.

static class Util
{
    /// <summary>
    /// Contains true, if we are in design mode of Visual Studio
    /// </summary>
    static bool _designMode;

    /// <summary>
    /// Initializes an instance of Util class
    /// </summary>
    static Util()
    {
        // design mode is true if host process is: Visual Studio, 
        // Visual Studio Express Versions (C#, VB, C++) or SharpDevelop
        var designerHosts = new List<string>() {
            "devenv", "vcsexpress", "vbexpress", "vcexpress", "sharpdevelop" };

        using (var process = System.Diagnostics.Process.GetCurrentProcess())
        {
            var processName = process.ProcessName.ToLower();
            _designMode = designerHosts.Contains(processName);
        }
    }

    /// <summary>
    /// Gets true, if we are in design mode of Visual Studio etc..
    /// </summary>
    public static bool DesignMode
    {
        get
        {
            return _designMode;
        }
    }
}

I always had troubles by using the built-in methods of .NET Framework like the System.ComponentModel.Component.DesignMode property. In ASP.NET and WPF there exist other methods to determine the design mode. For me the above one replaces all others and works like a charm.

Advertisements

One thought on “HOWTO: Detect design mode in WPF, ASP .NET and Windows Forms applications

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: