在 Visual Basic 6.0及以前版本中,都不支持对于控制台应用程序的编程。在VB.net开始后,VB能够支持最简单直接的命令行编程,其用法和C、C++等非常类似。以MSDN中列举的例子为例,可以很快速的完成控制台方式的程序。
' A "Hello, World!" program in Visual Basic.
Module Hello
Function Main(ByVal cmdArgs() As String) As Integer
'MsgBox("Hello, World!") ' Display message on computer screen.
console.clear()
console.writeline("这是一个控制台输出的测试程序。下面是你输入的命令行参数:")
dim sp as string
sp=""
for each s in cmdargs
sp=sp+s+" "
next
console.writeline(sp)
console.writeline("Hello, 你输入了" & cmdargs.length().tostring & "个变量,请按任意键结束程序。。。")
console.read()
Main=cmdargs.length()
End function
End Module
以上是一个完整的Console应用,把用户在命令行的输入显示出来,然后显示输入变量的个数。等待用户按任意键以后结束运行。
把以上的代码存在一个hello.vb的文本文件中,在命令行下使用VB的编译工具 VBC进行编译。如果没有出错,则不会有任何额外显示。如果出错,则显示出错误的内容和位置,修改后重新编译即可。最终生成hello.exe可执行文件。
运行hello.exe,并且在后面附加任意的内容,如"This is a test"作为该程序的输入参数,运行结果如下:
因为在程序中,选择的是带有返回值的Main函数形式,因此,最终把输入变量的个数返回给系统,可以使用 ERRORLEVEL这个环境变量来检测其结果。示例如下:echo %errorlevel%,显示的结果是4。
检查这个目录的全部文件,可以看到,源文件仅包含 hello.vb一个文件,大小613字节。编译后的运行程序也仅有7168字节,非常简练。对于一下简单工作,就不需要编写复杂的界面,只要完成核心工作即可。示例如下:
Imports System
Imports System.IO
Imports System.Text
Module TypeTxt
Public txtFile As StreamReader
Public strTmp As String
''' <summary>
''' 整个程序的主函数
''' </summary>
Function Main(ByVal cmdArgs() As String) As Integer
'MsgBox("此应用程序只是演示在控制台模式下读取文件的例子。")
Dim returnValue As Integer = 0
Dim sp As String = ""
' See if there are any arguments.
Console.WriteLine("这次输入了 " & cmdArgs.Length.ToString & " 个参数,分别列举如下:")
'If cmdArgs.Length > 0 Then
For argNum As Integer = 0 To UBound(cmdArgs, 1)
sp = cmdArgs(argNum)
Console.WriteLine(sp)
Next argNum
'End If
If cmdArgs.Length = 0 Then
Console.WriteLine("没有指定文件。程序退出。")
Main = -1
Exit Function
End If
If cmdArgs.Length > 1 Then sp = cmdArgs(1)
returnValue = readtxt(sp)
' Insert call to appropriate starting place in your code.
' On return, assign appropriate value to returnValue.
' 0 usually means successful completion.
'MsgBox("The application is terminating with error level " & CStr(returnValue) & ".")
Return returnValue
End Function
''' <summary>
''' 读文件并显示的过程
''' </summary>
Public Function readtxt(theFile As String) As Boolean
Dim curEnc As Encoding
Dim ansi As Encoding = Encoding.Default
Dim utf8 As Encoding = Encoding.UTF8
Dim ansiBytes As Byte()
Dim utf8Bytes As Byte()
Dim nLineNo As Long = 0
txtFile = New StreamReader(theFile)
If txtFile Is Nothing Then
Console.WriteLine("打开文件错误,退出。")
readtxt = False
Exit Function
End If
curEnc = txtFile.CurrentEncoding
Console.WriteLine()
Console.WriteLine("当前文件编码为:" & curEnc.ToString)
Do
strTmp = txtFile.ReadLine
‘MsgBox(strTmp)
nLineNo += 1
If strTmp Is Nothing Or nLineNo > 100 Then Exit Do
ansiBytes = ansi.GetBytes(strTmp)
utf8Bytes = Encoding.Convert(ansi, utf8, ansiBytes)
Dim uniChars(utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length) - 1) As Char
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, uniChars, 0)
Dim utfString As New String(uniChars)
Console.WriteLine(nLineNo.ToString() & ": " & uniChars)
Loop
txtFile.Close()
Console.WriteLine("显示完成,按任意键继续...")
Console.Read()
readtxt = True
End Function
End Module
以上是一个简单的显示文件内容的例子。但是因为Windows下ANSI编码和UTF-8编码的问题,对于UTF-8编码的文本,可以正确显示其中的汉字,但是如果使用ANSI编码格式,汉字则被显示为一连串的???。这个问题还没找到答案。
|