2013-04-29 64 views
1

如何檢測我的應用程序是從控制檯還是從「資源管理器」啓動?檢測Winform是否從控制檯啓動?

我想要做的是,如果應用程序從CMD啓動即可顯示CMD一點信息,只有當應用程序從CMD和不帶任何參數,所以我無法檢測的參數啓動。

該項目是Windowsform。

+2

http://stackoverflow.com/questions/3527555/how-can-you-determine-how-a-console-application-was-launched – Pacha 2013-04-29 06:39:13

+0

@Pacha三江源是解決我的問題! – ElektroStudios 2013-04-29 08:12:37

回答

0

VB代碼:

#Region " App Is Launched From CMD? " 

    ' [ App Is Launched From CMD? Function ] 
    ' 
    ' // By Elektro [email protected] 
    ' 
    ' 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 
相關問題