检查应用程序是否在Visual Studio中启动

检查应用程序是否在Visual Studio中启动

问题描述:

我正在开发一个安装系统级键盘
钩子的应用程序。当我在visual studio里面运行一个调试
build时,我不想安装这个钩子(否则它会挂起工作室
和最终的系统),我可以通过检查是否
DEBUG符号被定义。

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.

但是,当我调试应用程序的版本版本时,是
那里一种方法来检测它是否从内部的视觉
工作室开始避免同样的问题?这是非常讨厌的
重新启动工作室/电脑,只是因为我一直在
发布版本,并想修复一些错误使用调试器
忘记切换回到调试版本。

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.