Public Class Form1
之前加上
Imports System.IO
除了DirictoryInfo外,.net还提供了Directory类来操作目录,
大家在使用的时候,那个用着简单就用哪个。
我做了一个类似于 《
vb.net 教程 4-2 目录操作 DirectoryInfo 1
》中的窗体来说明:
获得目录信息:
Private Sub btnChooseFolder_Click(sender As Object,e As EventArgs) Handles btnChooseFolder.Click If FolderBrowserDialog1.ShowDialog <> DialogResult.OK Then Exit Sub End If Dim folderPath As String = FolderBrowserDialog1.SelectedPath txtFolderInfo.Text = "" txtFolderInfo.Text &= "路径:" & folderPath & ControlChars.CrLf txtFolderInfo.Text &= "创建日期:" & Directory.GetCreationTime(folderPath).ToString("yyyy-MM-dd") & ControlChars.CrLf txtFolderInfo.Text &= "最后修改日期:" & Directory.GetLastWriteTime(folderPath).ToString("yyyy-MM-dd") & ControlChars.CrLf End Sub
相比DirecotryInfo少了一些信息,不过可以获得一些别的信息:
Private Sub btnCurrent_Click(sender As Object,e As EventArgs) Handles btnCurrent.Click txtFolderInfo.Text = "" txtFolderInfo.Text &= "此程序所在目录:" & Directory.GetCurrentDirectory & ControlChars.CrLf txtFolderInfo.Text &= "d:\bb所在根目录:" & Directory.GetDirectoryRoot("d:\bb") & ControlChars.CrLf End SubGetCurrentDirecotry()方法获得当前运行程序所在路径。
Private Sub btnLogicalDrives_Click(sender As Object,e As EventArgs) Handles btnLogicalDrives.Click Dim LogicalDrives() As String LogicalDrives = Directory.GetLogicalDrives For Each drive As String In LogicalDrives txtFolderInfo.Text &= drive & ControlChars.CrLf Next End Sub
GetLogicalDrives()有点类似于《
vb.net 教程 4-1 获取磁盘驱动器信息 DriveInfo
》中用DriveInfo的GetDrives()方法,获得所有磁盘分区的Name。
运行时如图:
额,不用怀疑,我挂的硬盘分区有点多。
学习更多vb.net知识,请参看
vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256547.html