在 C# 代码中停止 Visual Studio 调试

Stop Visual Studio debugging in C# code

我有一些关闭应用程序的代码。但是,如果我 运行 Visual Studio 中的应用程序正在调试,我想停止调试而不是关闭我的计算机。任何建议:

代码:

#if DEBUG
            throw new Exception("Can't shutdown!");
#else
            try
            {
                this.Shutdown();
            }
            finally
            {
                Process.Start("shutdown.exe", "-r -f -t 0");
            }
#endif

尝试:

if (Debugger.IsAttached)
    throw new Exception("Can't shutdown!");
else
    try
    {
        this.Shutdown();
    }
    finally
    {
        Process.Start("shutdown.exe", "-r -f -t 0");
    }