检测是否从控制台启动了Winform?

时间:2013-04-29 06:29:49

标签: c# .net vb.net winforms cmd

如何检测我的应用程序是从控制台启动还是从“资源管理器”启动?

我想要做的是,如果从CMD启动应用程序然后在CMD上显示一些信息,只有从CMD启动应用程序并且没有任何参数,所以我无法检测到参数。

该项目是Windows形式。

1 个答案:

答案 0 :(得分:0)

VB代码:

#Region " App Is Launched From CMD? "

    ' [ App Is Launched From CMD? Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples:
    ' MsgBox(App_Is_Launched_From_CMD)
    ' If App_Is_Launched_From_CMD() Then Console.WriteLine("Help for this application: ...")

    Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
    Declare Function FreeConsole Lib "kernel32.dll" () As Boolean

    Private Function App_Is_Launched_From_CMD()
        If AttachConsole(-1) Then
            FreeConsole()
            Return True
        Else
            Return False
        End If
    End Function

#End Region