开发者

How to detect that C# Windows Forms code is executed within Visual Studio?

Is there a variable or开发者_JS百科 a preprocessor constant that allows to know that the code is executed within the context of Visual Studio?


Try Debugger.IsAttached or DesignMode property or get ProcessName or a combination, as appropriate

Debugger.IsAttached // or                                       
LicenseUsageMode.Designtime // or 
System.Diagnostics.Process.GetCurrentProcess().ProcessName

Here is a sample

public static class DesignTimeHelper {
    public static bool IsInDesignMode {
        get {
            bool isInDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime || Debugger.IsAttached == true;

            if (!isInDesignMode) {
                using (var process = Process.GetCurrentProcess()) {
                    return process.ProcessName.ToLowerInvariant().Contains("devenv");
                }
            }

            return isInDesignMode;
        }
    }
}


The DesignMode property isn't always accurate. We have had use this method so that it works consistently:

    protected new bool DesignMode
    {
        get
        {
            if (base.DesignMode)
                return true;

            return LicenseManager.UsageMode == LicenseUsageMode.Designtime;
        }
    }

The context of your call is important. We've had DesignMode return false in the IDE if running in an event under certain circumstances.


There is the DesignMode property for Components. It is handy when you use the Design Viewer of VS.

But when you talk about debugging in Visual Studio you need to use the Debugger.IsAttached property. Then, you can use

#if DEBUG
#endif

too


I think the simplest and most reliable way to determine if your extension is executed in the WinForms designer is to check the current process.

public static bool InVisualStudio() {
  return StringComparer.OrdinalIgnoreCase.Equals(
    "devenv", 
    Process.CurrentProcess.ProcessName);
}


I use this extension method:

internal static class ControlExtension
{
    public static bool IsInDesignMode(this Control control)
    {
        while (control != null)
        {
            if (control.Site != null && control.Site.DesignMode)
                return true;
            control = control.Parent;
        }
        return false;
    }
}


There is a DesignMode property that you can check but in my experience it's not always accurate. You could also check to see if the executable is DevEnv.exe

Take a look here. Might make this question a dup but it all depends on what you're trying to accomplish.


You can use this:

protected static bool IsInDesigner
{
    get { return (Assembly.GetEntryAssembly() == null); }
}


I use this code to distinguish whether it's running in Visual Studio or if it's deployed to customers.

if (ApplicationDeployment.IsNetworkDeployed) {
    // do stuff 
} else {
   // do stuff (within Visual Studio)
}

Works fine for me for ages. I skip some logic when inside Visual Studio (such as logging in to application etc).


I want to add to this that in Visual Studio 2022 with .Net 6, the process that actually opens winforms designers is called DesignToolsServer. From my experience so far, DesignMode works outside of the constructor and checking for ProcessName = "DesignToolsServer" works within the constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜