一、Path类
"../" 表示从目前工作目录再往上一层的目录位置,开始向下寻找。
二、选择目录文件
有三个控件可以使用:OpenFileDialog
SaveFileDialog
BrowFolderBrowserDialog
三、文件目录操作
左边是通用文件目录类,不需要对象即可创建,使用的是共享成员。适用:操作步骤少的,不需要过多引用
右边是特定文件目录类,需要创建对象才能操作。 适用:操作步骤多,引用多
FileSystemInfo类属性提供:文件或目录的相关信息,如:
Attributes FileSystemInfo的FileAttributes值
CreateTime Get/Set 建立时间
Exits 是否存在
Extension 扩展名(OnlyRead)
FullName 文件或目录完整名(含路径)(OlnyRead)
LastAccessTime
LastWriteTime
Name 文件或目录名
四、Directory类(通用)
1、CreateDirecotry 创建目录
2、Delete 删除目录
3、Move 移动(剪切)目录
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Directory.CreateDirectory("D:\1") Directory.Delete("D:\1") Directory.CreateDirectory("D:\2") Directory.Move("D:\2","D:\0517\2") End Sub End Class
4、列举目录内容
以前VB列举目录,还得自己编程一块代码,VB.net不用了,有专门的方法:
GetDirectories 取得当前目录下所有子目录
GetFiles 取得当前目录下所有文件(注意:不含子目录的文件)
GetFileSytemEnties 取回当前目录下所有目录及对应目录下所有文件
GetCurrentDirectory 返回当前工作目录
GetParent 返回父目录(DirectoryInfo对象)
GetLogicalDrives 返回所有逻辑磁盘名称(字串数组)
GetDirecotryRoot 返回根目录
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim p() As String = Directory.GetDirectories("D:\tools") '获取所有子目录 'Dim p() As String = Directory.GetDirectories("D:\tools","s*")'目录过滤,不分大小写 For i As Int32 = 0 To UBound(p) ListBox1.Items.Add(p(i)) Next End Sub
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim p() As String = Directory.GetFiles("D:\tools") '获取指定目录所有文件 'Dim p() As String = Directory.GetFiles("D:\tools","s*")'文件过滤,不分大小写 For i As Int32 = 0 To UBound(p) ListBox1.Items.Add(p(i)) Next End Sub
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim p() As String = Directory.GetFileSystemEntries("D:\tools") '获取指定目录(含子目录)及所有文件(含子目录 Array.Sort(p) '排序 For i As Int32 = 0 To UBound(p) ListBox1.Items.Add(p(i)) Next End Sub End Class
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim p() As String = Directory.GetFileSystemEntries("D:\tools") '获取指定目录(含子目录)及所有文件(含子目录 Array.Sort(p) '排序 For i As Int32 = 0 To UBound(p) ListBox1.Items.Add(p(i)) Next Dim s As String() = Directory.GetLogicalDrives Dim str As String = "" For i As Int32 = 0 To UBound(s) str &= vbCrLf & s(i) Next str &= vbCrLf & Directory.GetCurrentDirectory MessageBox.Show(str) End Sub End Class
5、获取、设置目录相关特性
GetCreationTime
SetCreationTime
GetLastAccessTime
SetLastAccessTime
GetLastWriteTime
SetLastWriteTime
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim s As String = "D:\1\1.txt" ListBox1.Items.Add(File.GetCreationTime(s)) ListBox1.Items.Add(File.GetLastAccessTime(s)) ListBox1.Items.Add(File.GetLastWriteTime(s)) File.SetLastWriteTime(s,New DateTime(2000,2,14,14)) ListBox1.Items.Add(File.GetCreationTime(s)) ListBox1.Items.Add(File.GetLastAccessTime(s)) ListBox1.Items.Add(File.GetLastWriteTime(s)) End Sub End Class
五、DirectoryInfo类(特定)
先通过New创建DirectoryInfo对象后,就可使用上面方法与成员。
也可以用前面Directory类中的CreateDirectory方法创建一个目录来建立一个DirectoryInfo对象。
1、操作目录:
Create 创建目录(注意创建前的判断目录是否已经存在)
Delete 删除目录
MoveTo 移动目录(更名目录也可用它,后面的File类更名一样这样使用)
Imports System.IO Imports System.IO.FileSystemInfo Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim fsi As New DirectoryInfo("d:\1\2") If Not fsi.Exists Then fsi.Create() End If Directory.SetCurrentDirectory("d:\1") If Not fsi.CreateSubdirectory("3").Exists Then fsi.MoveTo("D:\0517\2") End If End Sub End Class
2、列举目录
GetDirectories 返回所有子目录(DirecotryInfo对象集合)
GetFiles 返回当前当前目录下所有文件(FileInfo对象集合)
GetFileSystemInfos 返回所有子目录和对应目录下所有文件(FileSystemInfo对象集合)
Imports System.IO Imports System.IO.FileSystemInfo Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim myDirInfo As New DirectoryInfo("D:\tools") For Each s As FileSystemInfo In myDirInfo.EnumerateFileSystemInfos("S*") ListBox1.Items.Add(s.FullName.ToString) Next For Each d As DirectoryInfo In myDirInfo.GetDirectories("g*") ListBox1.Items.Add(d.FullName.ToString) Next End Sub End Class
3、目录属性
Exists 判断DirectoryInfo目录是否存在
Name 目录名
Parent 父目录对象
Root 根目录对象
4、注意点
创建后(未关闭程序),其它程序打开它时会发生异常。
Imports System.IO Imports System.IO.FileSystemInfo Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click File.Create("D:\1\1.txt") 'Dim a As FileStream = File.Create("D:\1\1.txt") '建立文件流 'a.Close() End Sub End Class
六、File类(通用)
同前面目录Directory类相似,方法成员如下:
操作文件:
Open
OpenRead
OpenWrite
Move
Delete
Copy
Exits
GetAttributes
GetCreationTime
GetLastAccessTime
GetLastWriteTime
SetCreationTime
SetLastAccessTime
SetLastWriteTime
需要注意的是,打开文件时有几个枚举:
Public Shared Function Open(Byval Path as String,Byval mode as Filemode,byval access as FileAcess,byval share as FileShare) As FileStream
1、返回的是FileStream
2、FileMode 以什么方式打开
Append 追加末端
Create 建立文件(若存在则覆盖)
CreateNew 建立全新(若存在该文件则出错)
Open 打开现存文件(不存在则出错)
Truncate 打开现存文件,并将长度截断为0(清空文件)
3、Access 所允许自己操作的行为:
Read
Write
ReadWrite
4、FileShare 此时其它程序允许操作的权限:
None 本程序独享(其它程序不能读也不能写)
Read 其它程序只读
Write 其它程序可写
ReadWrite 其它程序可读写
七、FileInfo类(特定)
同DiretoryInfo类似。需建立对象。
CopyTo
Create
Delete
MoveTo
Open
Directory 父目录
DiretoryName 完整名
Exists
Length 文件大小
Name 文件名
八、FileSystemWatcher文件系统监视器
vb.net中有这个控件,可以单独创建对象来操作。流程如下:
FileSystemWatcher属性主要有:
Path 所监视的目录
Filter 所监视的文件类型(过滤)
NotifyFilter 要监视的项目(大小、名称、建立时间、文件、目录等)
IncludeSubdirectories 是否含子目录
EnableRaisingEvents 是否启动监视
FileSsytemWatcher主要事件:
Changed 文件或目录变更时
Created 文件或目录建立时
Renamed 文件或目录更名时
Imports System.IO Public Class Form1 Public WithEvents fsw As New FileSystemWatcher Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load fsw.EnableRaisingEvents = False '关闭监视 fsw.Path = "D:\1" fsw.Filter = "*.txt" fsw.IncludeSubdirectories = False fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.Size End Sub Private Sub fsw_changed(sender As Object,e As FileSystemEventArgs) Handles fsw.Changed 'ListBox1.Items.Add(Now.ToString & " " & e.FullPath) '会遇线程 MessageBox.Show(e.FullPath) End Sub Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click fsw.EnableRaisingEvents = True '启动监视 End Sub End Class