是否存在一个变量或预处理器常量,该变量或预处理器常量使您知道代码是在Visual Studio上下文中执行的?

最佳答案

尝试Debugger.IsAttachedDesignMode属性或获取ProcessName或适当的组合

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

这是一个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;
        }
    }
}

关于c# - 如何检测C#Windows窗体代码在Visual Studio中执行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2427381/

10-10 09:38