《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理

上传人:E**** 文档编号:89408695 上传时间:2019-05-24 格式:PPT 页数:63 大小:287.50KB
返回 下载 相关 举报
《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理_第1页
第1页 / 共63页
《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理_第2页
第2页 / 共63页
《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理_第3页
第3页 / 共63页
《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理_第4页
第4页 / 共63页
《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理_第5页
第5页 / 共63页
点击查看更多>>
资源描述

《《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理》由会员分享,可在线阅读,更多相关《《Visual Basic .NET软件开发技术》-吴绍根-电子教案 第8章 文件管理及错误管理(63页珍藏版)》请在金锄头文库上搜索。

1、第8章 文件管理及错误管理,第一单元 文件管理,“文件”是计算机用于永久保存数据的字节的集合,文件中的数据是以数据流的形式存储的,可以在程序中对文件进行读、写操作。 VB.NET提供了两种用于文件操作的方式: 使用.NET的System.IO模型; 使用Visual Basic.NET的运行时函数;,使用.Net的System.IO进行文件操作,System.IO模型中包括了可对文件进行操作的基本的类,它们可以用来创建、拷贝、移动、删除文件,这些类都定义在System.IO名字空间中。 这个模型中比较常用的类包括: FileStream:用于打开文件,并提供与文件相关的信息,比如:文件的大小、

2、 文件的创建日期等; BinaryReader:对文件进行二进制方式的读; BinaryWriter:对文件进行二进制方式的写; StreamReader:以文本文件的方式对文件进行读操作; StreamWriter:以文本文件的方式对文件进行写操作; Directory:通过该类的对象,可以操作驱动器和文件夹; File:提供用于创建、复制、删除、移动和打开文件的静态方法;,FileStream类,使用 FileStream 类对文件系统上的文件进行读取、写入、打开和关闭操作。读写操作可以指定为同步或异步操作。 FileStream类使用枚举类型FileAccess、FileMode 和 F

3、ileShare来指定FileStream类的构造函数使用的标志,并影响创建、打开和共享基础文件的方式。,其中FileMode枚举类型有以下成员:,FileAccess枚举类型有以下成员:,FileShare枚举类型有以下成员:,举例:创建一个文件流对象,然后关闭这个流对象。 Imports System.IO Dim fs as new FileStream(“C:Tempmyfile.txt”, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read) fs.Close(),FileStream类的常用方法和属性:,Binar

4、yReader类和BinaryWriter类,BinaryReader类和BinaryWriter类可以用来读写二进制文件,其中,BinaryReader类用来从二进制文件中读取数据,而BinaryWriter类用来向二进制文件写入信息。,BinaryReader的常用方法和属性:,BinaryWriter的常用方法和属性:,举例,对文件D:TempMyfile.dat进行读写: Dim fs As New System.IO.FileStream(“C:tempmyfile.dat“, FileMode.OpenOrCreate) Dim bw As New BinaryWriter(fs)

5、 bw.Write(12.3) Dim br As New BinaryReader(fs) Dim d As Double br.BaseStream.Seek(0, SeekOrigin.Begin) d = br.ReadDouble() MessageBox.Show(d) fs.Close(),Dim fs As New System.IO.FileStream(“C:tempmyfile.dat“, FileMode.OpenOrCreate) Dim bw As New BinaryWriter(fs) bw.Write(12.3) bw.Write(50.2) Dim br A

6、s New BinaryReader(fs) Dim d As Double br.BaseStream.Seek(System.Runtime.InteropServices.Marshal.SizeOf(12.3), SeekOrigin.Begin) d = br.ReadDouble() MessageBox.Show(d) fs.Close(),StreamReader类及StreamWriter类,StreamReader类及StreamWriter类以字符的方式来读写流,这两个类在内部使用特定的编码在字符和字节之间进行转换。 StreamReader类的常用方法和属性:,Stre

7、amWriter类的常用方法和属性:,举例,对文件D:TempMyfile.txt进行读写: Dim fs As New System.IO.FileStream(“c:tempmyfile.txt“, FileMode.OpenOrCreate) Dim sw As New StreamWriter(fs) sw.WriteLine(“My Name is Bill“) sw.WriteLine(“Your Name is Bush“) sw.Flush() Dim sr As New StreamReader(fs) Dim s As String sr.BaseStream.Seek(0

8、, SeekOrigin.Begin) s = sr.ReadLine MessageBox.Show(s) fs.Close(),Dim fs As New System.IO.FileStream(“c:tempmyfile.txt“, FileMode.OpenOrCreate) Dim sw As New StreamWriter(fs) sw.WriteLine(“My Name is Bill“) sw.WriteLine(“Your Name is Bush“) sw.Write(23) sw.Write(45) sw.Flush() Dim sr As New StreamRe

9、ader(fs) Dim s As String sr.BaseStream.Seek(0, SeekOrigin.Begin) s = sr.ReadLine MessageBox.Show(s) s = sr.ReadLine MessageBox.Show(s) s = sr.ReadLine MessageBox.Show(CInt(s) + 1) fs.Close(),Directory类,Directory类中提供了一系列的用于对文件系统的目录进行操作的静态方法,这些方法可用于一些典型的操作,如:复制、移动、重命名、创建和删除目录。 也可将 Directory 类用于获取和设置与目

10、录的创建、访问及写入操作相关的 DateTime 信息。,Directory类常用的方法:,举例如下: Dim path As String = “C:MyDir“ Determine whether the directory exists. If Directory.Exists(path) Then MessageBox.Show(“That path exists already.“) Exit Sub End If Try to create the directory. Directory.CreateDirectory(path) MessageBox.Show(“The dir

11、ectory was created successfully “) Delete the directory. Directory.Delete(path) messagebox.Show (“The directory was deleted successfully“),File类,File类提供了用于创建、复制、删除、移动和打开文件的静态方法,可将File类用于典型的操作,如复制、移动、重命名、创建、打开、删除和追加到文件。 也可将File类用于获取和设置文件属性或有关文件创建、访问及写入操作的 DateTime 信息。,File类的常用方法:,举例如下: Dim path As St

12、ring = “c:tempMyTest.txt“ Dim path2 As String = path + “temp“ Dim fs As FileStream = File.Create(path) fs.Close() File.Copy(path, path2) File.Delete(path) MessageBox.Show(path & “copied to “ & path2),应用举例,Dim conn As New OleDb.OleDbConnection Dim path As String path = Application.StartupPath() & “da

13、ta.txt“ Dim fs As New System.IO.FileStream(path, FileMode.Open) Dim sr As New StreamReader(fs) sr.BaseStream.Seek(0, SeekOrigin.Begin) conn.ConnectionString = sr.ReadLine MessageBox.Show(conn.ConnectionString) conn.Open() If conn.State = ConnectionState.Open Then MessageBox.Show(“已成功连接数据库!“) conn.Cl

14、ose() End If,OpenFileDialog,OpenFileDialog类的常用方法和属性:,举例如下: Dim ofd As New OpenFileDialog ofd.CheckFileExists = False ofd.AddExtension = True ofd.DefaultExt = “txt“ ofd.ShowDialog() If ofd.FileName = “ Then Exit Sub End If Dim fs As New FileStream(ofd.FileName, FileMode.OpenOrCreate, FileAccess.ReadW

15、rite) Dim sr As New StreamReader(fs) MessageBox.Show(sr.ReadLine) MessageBox.Show(“成功“) fs.Close(),类似地,还有其它一些通用的对话框,下表给出Windows常用的对话框类(控件)及功能描述:,SaveFileDialog,举例如下: Dim sfd As New SaveFileDialog sfd.CheckFileExists = False sfd.AddExtension = True sfd.DefaultExt = “txt“ sfd.ShowDialog() If sfd.FileN

16、ame = “ Then Exit Sub End If Dim fs As New FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite) Dim sw As New StreamWriter(fs) sw.WriteLine(“文件中的数据“) sw.Flush() MessageBox.Show(“成功“) fs.Close(),使用Visual Basic.Net的run time 函数进行文件操作,为了保持VB.NET与以前VB6对文件操作的兼容性,在VB.NET中仍然保留了使用运行时的I/O函数来执行文件的操作. VB.NET的运行时函数允许三种类型的文件访问: 顺序访问文件; 这种方式的文件访问方式以顺序的、连续块的方式读写文本文件 注意,按这种方式只能访问文本文件 随机访问文件; 可以在任何时候读或者写文件的任何位置 但是,文件必须由同样长度的记录组成 二进制方式访问文件 ; 可以通过直接指定读写的开始位置及读写的长度来读写文件数据。,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号