在cmd窗口vb.net中显示命令行帮助

时间:2016-01-02 08:18:21

标签: vb.net cmd

我正在编写一个vb.net应用程序,我想在命令提示符窗口中显示我的命令行帮助,就像打开命令提示符并输入dir /?一样,它将显示命令行帮助dir。我希望我的应用程序在命令提示符窗口中响应,就像在命令提示符窗口中输入myapp.exe /?一样显示

Command-line arguments help:
/a Does stuff
/b Does this
/c Does that
/? Displays this help screen
...

myapp.exe /?命令下面。 任何人都可以这样做吗?

编辑:当您在命令提示符中输入denenv /?时,它将显示命令行参数列表和Visual Studio的使用情况(而不是启动新的Visual Studio进程)命令提示符窗口。我想做那样的事。我试着识别/?在My.Application.CommandLineArguments中,它需要我的应用程序的新进程,并且不能像Visual Studio那样在命令提示符下显示命令行参数列表和我的应用程序的使用。

2 个答案:

答案 0 :(得分:1)

你可以回复/?来自 MyApplication_Startup MyApplication_StartupNextInstance 的参数,并创建包含应用程序帮助的临时批处理文件,然后启动批处理文件并退出应用程序。

enter image description here

Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup

    If e.CommandLine(0) = "/?" Then 'Parameter to show help

        Dim BatFile As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\help.bat" 'Batch file path

        Dim Writer As New IO.StreamWriter(BatFile) 'Batch file contents
        Writer.WriteLine("ECHO OFF")
        Writer.WriteLine("CLS")
        Writer.WriteLine("TITLE My Application Help")
        Writer.WriteLine("ECHO My Application Help")
        Writer.WriteLine("echo.")
        Writer.WriteLine("ECHO /a Does stuff")
        Writer.WriteLine("ECHO /b Does this")
        Writer.WriteLine("ECHO /c Does that")
        Writer.WriteLine("echo.")
        Writer.WriteLine("PAUSE")
        Writer.Close()

        Process.Start(BatFile) 'Launch batch file

        e.Cancel = True 'Exit application

    End If

End Sub

批处理文件创建提示: http://www.makeuseof.com/tag/write-simple-batch-bat-file/

答案 1 :(得分:1)

直接写入命令提示符,您的exe类型必须是控制台应用程序,而不是Windows窗体应用程序,它由PE标头中的标志确定。

或者,您可以使用 AttachConsole Win32 API

写入控制台

enter image description here

    Imports System.Runtime.InteropServices

    <DllImport("kernel32.dll")> _
    Private Shared Function AttachConsole(dwProcessId As Integer) As Boolean
    End Function

    Private Const ATTACH_PARENT_PROCESS As Integer = -1

    Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup

        If e.CommandLine(0) = "/?" Then

            AttachConsole(ATTACH_PARENT_PROCESS)

            Console.WriteLine("My Application Help")
            Console.WriteLine("/a Does stuff")
            Console.WriteLine("/b Does this")
            Console.WriteLine("/c Does that")

            e.Cancel = True

        End If

    End Sub