|
项目开始时,先创建一个窗体。用户可以通过该窗体选择目录、从目录中选择文件以及选择要检索该文件的哪些信息。 创建项目 在“文件”菜单上,单击“新建项目”。 出现“新建项目”对话框。 在“项目类型”窗格中单击“Visual Basic 项目”,然后单击“模板”窗格中的“Windows 应用程序”。 在“名称”框中,键入 FileExplorer 以设置项目名称。 Visual Studio 将该项目添加到“解决方案资源管理器”中,Windows 窗体设计器随即打开。
显示当前目录 FileExplorer 应用程序需要一个起始点。因此 txtDirectoryTextBox 使用 My.Computer.FileSystem.CurrentDirectory 函数来返回并显示一个表示当前路径的字符串。 返回当前目录 双击该窗体,为 Form1_Load 事件创建一个事件处理程序。 代码编辑器打开。 添加以下代码,使 txtDirectoryTextBox 显示当前位置。 Visual Basic 复制代码 txtDirectory.Text = My.Computer.FileSystem.CurrentDirectory 运行该程序以确认返回的路径是否正确。 txtDirectory TextBox 显示当前目录。 更改目录 由于用户可能需要选择其他目录中的文件,因此该应用程序使用相同的属性来切换目录。若要更改到其他目录,用户需要在txtDirectoryTextBox 中输入一个新路径。 更改目录 双击窗体上的 btnSubmit 按钮,为该控件创建一个 Click 事件处理程序。 代码编辑器打开。 将以下代码添加到 Click 事件处理程序中。 Visual Basic 复制代码 Dim NewPath As String ' NewPath holds the path the user has entered. NewPath = txtDirectory.Text ' Change the location to NewPath. My.Computer.FileSystem.CurrentDirectory = NewPath 验证输入的路径是否有效 使用 Try...Catch 语句来捕获因提交空白或无效路径而引发的异常。 确保路径有效 在 btnSubmit_Click 事件中,将 Dim ErrorMessage As String 作为新行添加到代码行 Dim NewPath As String 之后。 在代码行 My.Computer.FileSystem.CurrentDirectory = NewPath 之前,添加一条 Try 语句(位于同一行),如下所示。如果您按回车键,代码编辑器将自动插入 Catch ex As Exception 和 End Try 语句。移除这些语句;您将在下一步中添加自己的语句。 Visual Basic 复制代码 Try 在代码行 My.Computer.FileSystem.CurrentDirectory = NewPath 后添加以下语句。 Visual Basic 复制代码 ' This checks to make sure the path is not blank. Catch ex As Exception When NewPath = "" ErrorMessage = "You must enter a path." ' This catches errors caused by a path that is not valid. Catch ErrorMessage = "You must enter a valid path. If trying " & _ "to access a different drive, remember to include the drive " & _ "letter." Finally ' Display the error message only if one exists. If ErrorMessage <> Nothing Then MsgBox(ErrorMessage) End If End Try 在组合框中显示目录的内容 若要允许应用程序显示当前目录的内容,可以使用 My.Computer.FileSystem.GetFiles 方法;该方法返回一个字符串的集合,这些字符串表示目录中文件的名称。可以将通配符与 GetFiles 一起使用,以便只选择特定模式的文件。在本例中,只返回扩展名为 .txt 的文件。 显示目录内容 在 btnSubmit_Click 事件的开头插入以下语句。 Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String). 在 End Try 行之后插入以下语句。 Visual Basic 复制代码 fileList = My.Computer.FileSystem.GetFiles( _ My.Computer.FileSystem.CurrentDirectory, _ FileIO.SearchOption.SearchTopLevelOnly, "*.txt") For Each foundFile As String In fileList lstFilePick.Items.Add(foundFile) Next 所收集的信息显示在 lstFilePickComboBox 中,您可以从中选择一个要检查的特定文件。 首先对一个不包含任何 .txt 文件的目录运行该应用程序,然后对一个包含多个 .txt 文件的目录运行该应用程序,对其进行测试。第一种情况下,该应用程序显示相应的错误信息。第二种情况下,该应用程序在 ComboBox 中创建一个列表,列出在 txtDirectoryTextBox 中指定的目
录内的所有 .txt 文件。 使用户可以选择要检查的文件 虽然 lstFilePickComboBox 显示一个目录中的所有文件,但用户很可能需要选择并检查某个特定文件。 允许选择特定文件 为 btnExamine_Click 事件创建一个 Click 事件处理程序,然后添加以下代码来确认文件选择。 Visual Basic 复制代码 Dim thisFile As System.IO.FileInfo thisFile = My.Computer.FileSystem.GetFileInfo(CStr(lstFilePick.SelectedItem)) 使用户可以决定收集哪些信息 现在,文件已经显示在 lstFilePickComboBox 中,可以添加代码让用户能够指定要报告的信息。例如,一个用户可能只需要了解上次访问文件的日期,而另一个用户可能还需要了解文件的大小。用户可以通过选择或清除复选框(chkLastAccess、chkFileLength)来自定义结果。 显示特定信息 在 btnExamine_Click 事件开头中的 (lstFilePick.SelectedItem) 语句之后声明这些变量: Visual Basic 复制代码 Dim stringlength As String stringLength = "The file's length, in bytes, is: " Dim stringLastAccess As String stringLastAccess = "The file was last accessed on: " Dim LastAccess As Date Dim Length As Long Dim FirstLine As String = "" Dim FinalString As String = "" Dim NewName As String NewName = CType(lstFilePick.SelectedItem, String) If NewName = Nothing Then MsgBox("You must select a file to examine.") Exit Sub End If My.Computer.FileSystem.GetFileInfo 方法返回一个 FileInfo 对象,可以查询该对象以获取有关某个文件的信息。 将以下代码添加到 btnExamine_Click 事件的末尾。 Visual Basic 复制代码 ' Check last access time. If chkLastAccess.Checked = True Then LastAccess = thisFile.LastAccessTime End If LastAccessTime 属性用于确定上次访问该文件的时间。返回的 Date 值表示创建或上次修改该文件的日期和时间。 将以下代码添加到 btnExamine_Click 事件的末尾。 Visual Basic 复制代码 ' Check Length. If chkFileLength.Checked = True Then Length = thisFile.Length End If Length 属性用于确定文件的长度。它返回一个 Long 值,以字节为单位指定该文件的长度。 显示结果 为使该应用程序功能完善,可以添加一个 MsgBox 来报告所收集的信息。 显示结果 在确定是否选中 chkLastAccessCheckBox 的 If 语句的末尾,将以下语句添加到最终的 End If 之前。 Visual Basic 复制代码 ' Add to the messagebox. FinalString = FinalString & stringLastAccess & LastAccess & "." _ & vbCrLf 在确定是否选中 chkFileLengthCheckBox 的 If 语句的末尾,将以下语句添加到最终的 End If 之前。 Visual Basic 复制代码 ' Add to the messagebox. FinalString = FinalString & stringlength & CStr(Length) & "." _ & vbCrLf 在确定是否选中 chkFirstLineCheckBox 的 If 语句的末尾,将以下语句添加到最终的 End If 之前。 Visual Basic 复制代码 ' Add to the messagebox. FinalString &= FirstLine & vbCrLf
共2页: 上一页 1 [2] 下一页
|