vb.net 教程 4-5 文件操作 File 1
来源:互联网 发布:everything是什么软件 编辑:程序博客网 时间:2024/06/14 18:00
.Net对文件的操作主要通过System.Io命名空间进行的,包括驱动器、文件夹、文件信息的获取、简单操作,以及文件和数据流的读写等。
在本章教程中需要引用System.Io,请在窗体的所有代码之前,例如Form1窗体的代码
Public Class Form1
之前加上
Imports System.IO
除了FileInfo外,.net还提供了File类来操作文件,
就像Directory与DirectoryInfo是一对类似的类,File和FileInfo也很像,同样,不同于FileInfo,File没有提供属性,只有方法,而且方法是静态的,也就是直接可以用的。
还是之前的话,哪个用着简单就用哪个。
也是和《vb.net 教程 4-3 文件操作 FileInfo 1》中类似的窗体来说明FIle类的使用:
主要的代码:
获得文件信息,和介绍FileInfo时的代码差不多:
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
嗯,学到这里。这下可以用代码恢复被优盘病毒修改为系统、隐藏的文件了,不需要再在cmd下敲attrib了。
学习更多vb.net知识,请参看 vb.net 教程 目录
阅读全文
0 0
- vb.net 教程 4-5 文件操作 File 1
- vb.net 教程 4-5 文件操作 File 2
- vb.net 教程 4-3 文件操作 FileInfo 1
- vb.net 教程 4-3 文件操作 FileInfo 2
- vb.net 教程 4-4 目录操作 Directory 1
- vb.net 教程 4-2 目录操作 DirectoryInfo 5
- vb.net 教程 4-2 目录操作 DirectoryInfo 1
- [VB.NET]文件操作
- VB.net文件操作
- (vb.net)操作Excel文件1
- VB.net ini文件操作
- vb.net操作excel文件
- vb.net操作excel文件
- vb.net 教程 4-2 目录操作 DirectoryInfo 4
- vb.net 教程 4-4 目录操作 Directory 2
- vb.net 教程 8-3 数据库操作5
- vb.net 教程 4-2 目录操作 DirectoryInfo 2
- vb.net 教程 4-2 目录操作 DirectoryInfo 3
- struts2获取url参数问题
- 范数
- JAVA泛型
- scrapy一些介绍
- ajax跨域的头部定制
- vb.net 教程 4-5 文件操作 File 1
- Java经典算法40例(九)
- codeforces792E Colored Ball(贪心加枚举)
- 基于TCP的server和client编写。
- 梯度下降
- nginx反向代理原理
- 详解Response、Request一
- 微信小程序中 input空间输入文字如何清空
- 自定义圆圈进度条