检查应用程序是否从 Visual Studio 调试会话中启动

检查应用程序是否从 Visual Studio 调试会话中启动

问题描述:

我正在开发一个安装系统范围键盘的应用程序钩.我不想在运行调试时安装这个钩子从视觉工作室内部构建(否则它会挂起工作室最终是系统),我可以通过检查已定义调试符号.

I am working on an application that installs a system wide keyboard hook. I do not want to install this hook when I am running a debug build from inside the visual studio (or else it would hang the studio and eventually the system), and I can avoid this by checking if the DEBUG symbol is defined.

但是,当我调试应用程序的 release 版本时,有一种方法可以检测它是否已从内部视觉启动工作室避免同样的问题?不得不这样做很烦人重新启动工作室/计算机,只是因为我一直在工作发布版本,并希望使用调试器修复一些错误忘记切换回调试版本.

However, when I debug the release version of the application, is there a way to detect that it has been started from inside visual studio to avoid the same problem? It is very annoying to have to restart the studio/the computer, just because I had been working on the release build, and want to fix some bugs using the debugger having forgotten to switch back to the debug build.

目前我使用这样的东西来检查这种情况:

Currently I use something like this to check for this scenario:

System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
string moduleName = currentProcess.MainModule.ModuleName;
bool launchedFromStudio = moduleName.Contains(".vshost");

我称之为蛮力方式",这在我的设置中有效,但我想知道是否有另一种(更好的)方法来检测这种情况.

I would call this the "brute force way", which works in my setting, but I would like to know whether there's another (better) way of detecting this scenario.