Public Class Form1
之前加上
Imports System.IO
除了FileInfo外,.net还提供了File类来操作文件,
就像Directory与DirectoryInfo是一对类似的类,File和FileInfo也很像,同样,不同于FileInfo,File没有提供属性,只有方法,而且方法是静态的,也就是直接可以用的。
还是之前的话,哪个用着简单就用哪个。
也是和《
vb.net 教程 4-3 文件操作 FileInfo 1
》中类似的窗体来说明FIle类的使用:
主要的代码:
Private Sub btnChooseFile_Click(sender As Object,e As EventArgs) Handles btnChooseFile.Click If OpenFileDialog1.ShowDialog <> DialogResult.OK Then Exit Sub End If Dim FilePath As String = OpenFileDialog1.FileName txtFileInfo.Text = "" txtFileInfo.Text &= "文件路径:" & FilePath & ControlChars.CrLf txtFileInfo.Text &= "创建日期:" & File.GetCreationTime(FilePath).ToString("yyyy-MM-dd") & ControlChars.CrLf txtFileInfo.Text &= "修改日期:" & File.GetLastWriteTime(FilePath).ToString("yyyy-MM-dd") & ControlChars.CrLf txtFileInfo.Text &= "属性:" & getFolderAttr(File.GetAttributes(FilePath)) & ControlChars.CrLf End Sub Function getFolderAttr(ByVal attr As Integer) As String Dim strAttr As String = "" If (attr And FileAttributes.Archive) Then strAttr &= " 备份" If (attr And FileAttributes.Compressed) Then strAttr &= " 压缩" If (attr And FileAttributes.Directory) Then strAttr &= " 目录" If (attr And FileAttributes.Encrypted) Then strAttr &= " 加密" If (attr And FileAttributes.Hidden) Then strAttr &= " 隐藏" If (attr And FileAttributes.Normal) Then strAttr &= " 正常" If (attr And FileAttributes.Offline) Then strAttr &= " 脱机" If (attr And FileAttributes.ReadOnly) Then strAttr &= " 只读" If (attr And FileAttributes.System) Then strAttr &= " 系统" If (attr And FileAttributes.Temporary) Then strAttr &= " 临时" Return strAttr End Function
Private Sub btnAttr_Click(sender As Object,e As EventArgs) Handles btnAttr.Click Dim filepath As String = "d:\bb.jpg" If File.Exists(filepath) = False Then Exit Sub Dim attr As Integer = File.GetAttributes(FilePath) If (attr And FileAttribute.ReadOnly) = FileAttribute.ReadOnly Then If cbReadonly.Checked = False Then attr = attr Xor FileAttribute.ReadOnly Else If cbReadonly.Checked = True Then attr = attr Or FileAttribute.ReadOnly End If If (attr And FileAttribute.Hidden) = FileAttribute.Hidden Then If cbHidden.Checked = False Then attr = attr Xor FileAttribute.Hidden Else If cbHidden.Checked = True Then attr = attr Or FileAttribute.Hidden End If If (attr And FileAttribute.System) = FileAttribute.System Then If cbSys.Checked = False Then attr = attr Xor FileAttribute.System Else If cbSys.Checked = True Then attr = attr Or FileAttribute.System End If File.SetAttributes(filepath,attr) End Sub
学习更多vb.net知识,请参看
vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256544.html