这里不是指用户控件中检测是用于开发环境还是用户运行环境,而是指“程序”比如我希望程序在Main运行时,检测如果是开发环境,就不显示用户登录窗口,直接进入主界面。如果是用户运行环境,就显示用户登录窗口。原来在VB中,可以用如下代码实现:
If GetModuleHandle("VB6.EXE") = 0 Then
bRunInVB6 = False
Else
bRunInVB6 = True
End If到了VS2008,我用以下方法不成功
static class Program
{
// API声明
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//以下我加入的代码
if (GetModuleHandle("devenv.exe").ToInt32() == 0)
{
MessageBox.Show("显示用户登录界面!");
}
else
{
MessageBox.Show("跳过!");
}
//结束 Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
If GetModuleHandle("VB6.EXE") = 0 Then
bRunInVB6 = False
Else
bRunInVB6 = True
End If到了VS2008,我用以下方法不成功
static class Program
{
// API声明
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//以下我加入的代码
if (GetModuleHandle("devenv.exe").ToInt32() == 0)
{
MessageBox.Show("显示用户登录界面!");
}
else
{
MessageBox.Show("跳过!");
}
//结束 Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
如果判断的话,调试的时候应该会启动宿主程序“***.vshost.exe”
把
if (GetModuleHandle("devenv.exe").ToInt32() == 0)
改为
if (GetModuleHandle("T1.vshost.exe").ToInt32() == 0)
即可达到我希望的效果了。