参考MSDN2008得出的代码,大家下载工程文件后将txt文件拖动到exe上执行可看到效果。
Imports System.IO Imports System.Text Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Try Dim FS As FileStream,CmdArg As System.Collections.ObjectModel.ReadOnlyCollection(Of String) CmdArg = My.Application.CommandLineArgs If CmdArg.Count = 0 Then Exit Sub Dim FI As New FileInfo(CmdArg(0).Replace("""","")) If FI.Exists Then FS = New FileStream(FI.FullName,FileMode.Open) Dim arrFile(FI.Length - 1) As Byte FS.Read(arrFile,FI.Length) TextBox1.Text = ASCIIEncoding.GetEncoding("GB2312").GetString(arrFile) FS.Close() FS = Nothing End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
ApplicationEvents.vb
Imports System.IO Imports System.Text Namespace My ' 以下事件可用于 MyApplication: ' ' Startup: 应用程序启动时在创建启动窗体之前引发。 ' Shutdown: 在关闭所有应用程序窗体后引发。如果应用程序异常终止,则不会引发此事件。 ' UnhandledException: 在应用程序遇到未处理的异常时引发。 ' StartupNextInstance: 在启动单实例应用程序且应用程序已处于活动状态时引发。 ' NetworkAvailabilityChanged: 在连接或断开网络连接时引发。 Partial Friend Class MyApplication Private Sub MyApplication_StartupNextInstance(ByVal sender As Object,ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance For Each s As String In e.CommandLine Try Dim FI As New FileInfo(s.Replace("""","")) If FI.Exists Then Dim FS As FileStream = New FileStream(FI.FullName,FileMode.Open) Dim arrFile(FI.Length - 1) As Byte FS.Read(arrFile,FI.Length) Form1.TextBox1.Text = ASCIIEncoding.GetEncoding("GB2312").GetString(arrFile) FS.Close() FS = Nothing End If Catch ex As Exception MsgBox(ex.Message) End Try Next End Sub End Class End Namespace
工程下载地址:http://www.mafom.com/upload/201009071613522244.rar