1 / 40

Visual Basic 程序设计教程

Visual Basic 程序设计教程. 第 9 章 文 件 管 理. 9.1 公共对话框 9.1.1 添加“公共对话框”控件 9.1.2 使用“公共对话框” 9.2 数据文件 9.2.1 访问顺序文件 9.2.2 访问随机文件 9.2.3 访问二进制文件. 9.3 文件系统控件 9.3.1 驱动器列表框 9.3.2 目录列表框 9.3.3 文件列表框 9.4 文件系统对象 9.4.1 文件系统对象的概念 9.4.2 使用文件系统对象编程 9.4.3 管理驱动器 9.4.4 管理文件夹

nickan
Download Presentation

Visual Basic 程序设计教程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 程序设计教程

  2. 第9章 文 件 管 理 9.1公共对话框 9.1.1 添加“公共对话框”控件 9.1.2 使用“公共对话框” 9.2数据文件 9.2.1 访问顺序文件 9.2.2 访问随机文件 9.2.3 访问二进制文件 9.3文件系统控件 9.3.1 驱动器列表框 9.3.2 目录列表框 9.3.3 文件列表框 9.4文件系统对象 9.4.1 文件系统对象的概念 9.4.2 使用文件系统对象编程 9.4.3 管理驱动器 9.4.4 管理文件夹 9.4.5 管理文件

  3. 9.1公共对话框 9.1.2 使用“公共对话框 【例9-1】使用公共对话框控件的例子 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性。 (2) 编写命令按钮数组Command()的Click事件代码: Private Sub Command1_Click(Index As Integer) n = Index Select Case n Case 0 CommonDialog1.Filter = "所有文件(*.*)|*.*|文本文件(*.TXT)|*.tet"

  4. CommonDialog1.FilterIndex = 1 CommonDialog1.ShowOpen Text1.Text = CommonDialog1.FileName Frame1.Caption = "从打开对话框返回" Case 1 CommonDialog1.ShowSave Text1.Text = CommonDialog1.FileName Frame1.Caption = "从另存为对话框返回" Case 2 CommonDialog1.ShowColor Text1.Text = "从颜色对话框返回" Text1.ForeColor = CommonDialog1.Color

  5. Frame1.Caption = "从颜色对话框返回" Case 3 CommonDialog1.Flags = 3 Or 256 CommonDialog1.ShowFont With Text1 .FontName = CommonDialog1.FontName .FontSize = CommonDialog1.FontSize .FontStrikethru = CommonDialog1.FontStrikethru .FontBold = CommonDialog1.FontBold .FontItalic = CommonDialog1.FontItalic .FontUnderline = CommonDialog1.FontUnderline .ForeColor = CommonDialog1.Color End With Text1.Text = "从字体对话框返回" Frame1.Caption = "从字体对话框返回" End Select End Sub

  6. 9.2数据文件 9.2.1 访问顺序文件 1. 顺序文件的打开与关闭 Open〈文件名〉For {Input | Output | Append} As〈文件号〉[Len = buffersize] Close〈文件号1〉[,〈文件号2〉…] 2. 顺序文件的读取操作 Line Input#〈文件号〉,〈变量名〉 Input( Length,〈文件号〉) Input#〈文件号〉,〈变量名1〉[,〈变量名2〉… 3. 顺序文件的写入操作 Print #〈文件号〉,〈数据〉[{ , | ; }] Write #〈文件号〉,〈数据〉

  7. 【例9-2】设计一个简易文本编辑器,具有创建、编辑、保存普通文本文件的功能,如图 9-8所示。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性 (2) 编写代码 图9-8 简易的文本编辑器

  8. Private Sub Form_Resize() With Text1 .Left = 0 .Top = 0 .Height = Form1.ScaleHeight .Width = Form1.ScaleWidth – Picture1.Width End With End Sub 然后编写命令按钮组的Click事件代码: Private Sub Command1_Click(Index As Integer) n = Index Select Case n

  9. Case 0 ' 新建 Text1.Text = "" Form1.Caption = "未命名" Case 1 ' 打开 CommonDialog1.ShowOpen ' 显示"打开"公共对话框 fname = CommonDialog1.FileName If fname <> "" Then Text1.Text = "" Open fname For Input As #1 b = "" Do Until EOF(1) Line Input #1, nextline b = b & nextline & Chr(13) & Chr(10) Loop Close #1 Text1.Text = b End If Form1.Caption = fname

  10. Case 2 ' 保存 If Form1.Caption = "未命名" Or Form1.Caption = "" Then CommonDialog1.ShowSave ' 显示"另存为"公共对话框 fname = CommonDialog1.FileName Else fname = Form1.Caption End If If fname <> "" Then Open fname For Output As #1 Print #1, Text1.Text Close #1 End If

  11. Case 3 ' 另存 CommonDialog1.ShowSave ' 显示“另存为”公共对话框 fname = CommonDialog1.FileName If fname <> "" Then Open fname For Output As #1 Print #1, Text1.Text Close #1 End If Case 4 ' 关闭 Text1.Text = "" End End Select Text1.SetFocus End Sub

  12. 9.2.2 访问随机文件 1. 随机文件的打开与关闭 随机文件的打开仍用Open语句,但其语法不同: Open〈文件名〉[For Random] As〈文件号〉Len =〈记录长度〉 2. 随机文件的读写操作 (1) 把记录读入变量,使用Get #语句: Get #〈文件号〉,〈记录号〉,〈变量名〉 (2) 使用Put #语句可以把数据写入或替换随机文件中的记录: Put #〈文件号〉,〈记录号〉,〈变量名〉 【例9-3】利用随机文件保存学生的成绩,可以输入学生的学号、姓名以及3门功课的成绩,浏览或删除数据,如图9-9所示。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性。 (2) 编写代码。

  13. 首先在窗体的通用过程段创建用户定义类型并声明变量:首先在窗体的通用过程段创建用户定义类型并声明变量: Private Type cj xm As String * 6 xh As String * 6 sx As Integer yw As Integer wy As Integer End Type Private da As cj

  14. 编写窗体的Load事件代码,使之具有显示数据的功能:编写窗体的Load事件代码,使之具有显示数据的功能: Private Sub Form_Load() Dim lastrec As Integer, sx As Single Dim yw As Single Open "xsda2.dat" For Random As #1 Len = Len(da) ' 打开随机文件 lastrec = LOF(1) / Len(da) List1.Clear For n = 1 To lastrec Get #1, n, da With da xh = Format(.xh, "@@@@@@") xm = Format(RTrim(.xm), "@@@@") yw = Format(.yw, "####")

  15. wy = Format(.wy, "####") sx = Format(.sx, "####") msg = xh & xm & " " & yw & " " & wy & " " & sx End With List1.AddItem msg Next Close #1 ' 关闭随机文件 If List1.ListIndex > -1 Then Command2.Enabled = True Else Command2.Enabled = False End If End Sub

  16. 编写“新记录”命令按钮Command1的Click事件代码:编写“新记录”命令按钮Command1的Click事件代码: Private Sub Command1_Click() For i = 0 To 4 Text1(i).Text = "" Next Text1(0).SetFocus End Sub 编写“添加”命令按钮Command2的Click事件代码: Private Sub Command2_Click() Dim lastrec As Integer With da .xh = Text1(0).Text .xm = Text1(1).Text .yw = Val(Text1(2).Text)

  17. .wy = Val(Text1(3).Text) .sx = Val(Text1(4).Text) End With Open "xsda2.dat" For Random As #1 Len = Len(da) ' 打开随机数据文件 lastrec = LOF(1) / Len(da) Put #1, lastrec + 1, da Close #1 Call Form_Load Text1(0).SetFocus End Sub 编写“删除”命令按钮Command3的Click事件代码: Private Sub Command3_Click() Dim lastrec As Integer recnum = List1.ListIndex + 1

  18. Open "rec.tem" For Random As #1 Len = Len(da) ' 打开临时随机文件 Open "xsda2.dat" For Random As #2 Len = Len(da) ' 打开随机数据文件 lastrec = LOF(2) / Len(da) For n = 1 To lastrec If n <> recnum Then Get #2, n, da Put #1, , da Else Get #2, n, da With da Text1(0).Text = .xh Text1(1).Text = .xm Text1(2).Text = .yw

  19. Text1(3).Text = .wy Text1(4).Text = .sx End With End If Next Close #1 Close #2 Kill "xsda2.dat" Name "rec.tem" As "xsda2.dat" Call Form_Load Text1(0).SetFocus End Sub

  20. 编写列表框List1的Click事件代码: Private Sub List1_Click() If List1.ListIndex > –1 Then Command3.Enabled = True Else Command3.Enabled = False End If End Sub 9.2.3 访问二进制文件 1. 创建和打开二进制文件 打开和创建一个二进制文件都是用同一个Open语句来实现,其格式如下: Open〈文件名〉For Binary As〈文件号〉 2.读写二进制文件 Get #〈文件号〉,〈字节数〉,〈变量名〉 Put #〈文件号〉,〈字节数〉,〈变量名〉

  21. 3. 关闭二进制文件 Close #〈文件号〉 9.3文件系统控件 【例9-4】使用文件系统控件制作简易的文本浏览器。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性。 (2) 编写代码。 编写驱动器列表框Drive1的Change事件代码: Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub

  22. 编写文件表框File1的Click事件代码: Private Sub File1_Click() ChDrive Drive1.Drive ChDir Dir1.Path Text1.Text = "" Open File1.FileName For Input As #1 b = "" Do Until EOF(1) Line Input #1, nextline b = b & nextline & Chr(13) & Chr(10) Loop Close #1 Text1.Text = b End Sub

  23. 9.4.2 使用文件系统对象编程 (1) 将一个变量声明为FileSystemObject对象类型: Dim〈变量名〉As New FileSystemObject (2) 使用CreateObject方法来创建一个FileSystemObject对象: Set〈变量名〉= CreateObject("Scripting.FileSystemObject") 【例9-5】查看驱动器信息的小程序。如图9-11所示。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性。 (2) 编写程序代码。

  24. 编写Drive1的Change事件代码: Private Sub Drive1_Change() Dim fso As New FileSystemObject, drv As Drive, s As String mdrive = Drive1.Drive Set drv = fso.GetDrive(fso.GetDriveName(mdrive)) s = "驱动器 " & UCase(mdrive) & Chr(13) s = s & drv.VolumeName & vbCrLf & Chr(13) s = s & "最大空间: " & FormatNumber(drv.TotalSize / 1024, 0) s = s & " Kb" & vbCrLf & Chr(13) & Chr(13) s = s & "剩余空间: " & FormatNumber(drv.FreeSpace / 1024, 0) s = s & " Kb" & vbCrLf MsgBox s End Sub

  25. 【例9-6】编写一个文件夹管理器小程序。可以创建、复制、移动、删除文件夹,还可以对文件夹进行更名的操作,如图9-12所示。【例9-6】编写一个文件夹管理器小程序。可以创建、复制、移动、删除文件夹,还可以对文件夹进行更名的操作,如图9-12所示。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性。 (2) 编写程序代码。

  26. 首先在通用过程中声明对象变量: Dim fso As New FileSystemObject, drv As Drive, fldr As Folder, s As String 编写Drive1的Change事件代码: Private Sub Drive1_Change() Dir1.Path = Drive1.Drive ChDrive Drive1.Drive: ChDir Dir1.Path End Sub 编写Drive2的Change事件代码: Private Sub Drive2_Change() Dir2.Path = Drive2.Drive End Sub 编写Dir1的Change事件代码: Private Sub Dir1_Change() ChDir Dir1.Path End Sub

  27. 编写命令按钮数组Command1的Click事件代码: Private Sub Command1_Click(Index As Integer) Set drv = fso.GetDrive(Drive1.Drive) Select Case Index Case 0 ' 新建 Set fldr = fso.GetFolder(Dir1.Path) msg = "请输入新创建的文件夹名:" msg = msg & Chr(13) & Chr(13) & "当前的文件夹名为:" a = InputBox(msg & fldr.Path, "创建新文件夹", "新文件夹") If Len(Trim(a)) <> 0 Then Set fldr = fso.CreateFolder(a) Case 1 ' 复制 Set fldr = fso.GetFolder(Dir1.List(Dir1.ListIndex)) fldr.Copy IIf(Right(Dir2.Path, 1) = "\", Dir2.Path, Dir2.Path & "\") Case 2 ' 移动

  28. Set fldr = fso.GetFolder(Dir1.List(Dir1.ListIndex)) If Drive1.Drive <> Drive2.Drive Then MsgBox “不能在不同的驱动器间移动!”, 48, “移动” Set fldr = fso.GetFolder(Dir1.List(Dir1.ListIndex)) If Drive1.Drive <> Drive2.Drive Then MsgBox “不能在不同的驱动器间移动!”, 48, “移动” Else fldr.Move IIf(Right(Dir2.Path, 1) = "\", Dir2.Path, Dir2.Path & "\") End If Case 3 ' 删除 Set fldr = fso.GetFolder(Dir1.List(Dir1.ListIndex)) If Dir1.ListIndex = -1 Then MsgBox "不能删除正打开的文件夹!", 48, "删除" Else

  29. msg = "真要删除以下文件夹吗?" & Chr(13) & Chr(13) a = MsgBox(msg & fldr.Path, 1 + 32 + 256, "删除文件夹") If a = 1 Then fldr.Delete End If Case 4 ‘ 更名 Set fldr = fso.GetFolder(Dir1.List(Dir1.ListIndex)) msg = "请输入新的文件夹名:" msg = msg & Chr(13) & Chr(13) & "文件夹原名为:" a = InputBox(msg & fldr.Path, "文件夹更名", fldr.Path) If Len(Trim(a)) <> 0 Then fldr.Name = a End Select Dir2.Refresh Dir1.Refresh End Sub

  30. 【例9-7】编写一个文件管理器小程序。可以复制、移动、删除文件,还可以查看文件的属性和对文件进行更名的操作,如图9-13所示。【例9-7】编写一个文件管理器小程序。可以复制、移动、删除文件,还可以查看文件的属性和对文件进行更名的操作,如图9-13所示。 设计步骤如下: (1) 建立应用程序用户界面与设置对象属性与例9-6相仿,只是在两个框架中多了一个文件列表框File1(File2),参见图9-13。 (2) 编写程序代码。 首先在通用过程中声明对象变量: Dim fso As New FileSystemObject, drv As Drive Dim fldr As Folder, fil As File, s As String

  31. 编写Drive1的Change事件代码: Private Sub Drive1_Change() Dir1.Path = Drive1.Drive ChDrive Drive1.Drive: ChDir Dir1.Path End Sub 编写Drive2的Change事件代码: Private Sub Drive2_Change() Dir2.Path = Drive2.Drive End Sub 编写Dir1的Change事件代码: Private Sub Dir1_Change() File1.Path = Dir1.Path ChDir Dir1.Path End Sub

  32. 编写Dir2的Change事件代码: Private Sub Dir2_Change() File2.Path = Dir2.Path End Sub 编写命令按钮数组Command1的Click事件代码: Private Sub Command1_Click(Index As Integer) On Error GoTo ErrorHandler ' 打开错误处理程序 Set drv = fso.GetDrive(Drive1.Drive) Select Case Index Case 0 ' 属性 Set fil = fso.GetFile(File1.FileName) msg = "最后修改日期:" & fil.DateLastModified & Chr(13) msg = msg & "文件名:" & fil.Name & Chr(13)

  33. msg = msg & "文件长度:" & fil.Size & Chr(13) msg = msg & "文件类型:" & fil.Type & Chr(13) MsgBox msg, , "文件管理器" Case 1 ' 复制 Set fil = fso.GetFile(File1.FileName) fil.Copy IIf(Right(Dir2.Path, 1) = "\", Dir2.Path, Dir2.Path & "\") Case 2 ' 移动 Set fil = fso.GetFile(File1.FileName) If Drive1.Drive <> Drive2.Drive Then MsgBox "不能在不同的驱动器间移动!", 48, "移动" Else fil.Move IIf(Right(Dir2.Path, 1) = "\", Dir2.Path, Dir2.Path & "\") End If Case 3 ' 删除

  34. Set fil = fso.GetFile(File1.FileName) msg = "真要删除以下文件吗?" & Chr(13) & Chr(13) a = MsgBox(msg & fil, 1 + 32 + 256, "删除文件") If a = 1 Then fil.Delete Case 4 ' 更名 Set fil = fso.GetFile(File1.FileName) msg = "请输入新的文件名:" & Chr(13) & Chr(13) & "文件原名为:" a = InputBox(msg & fil, "文件更名", fil.Name) If Len(Trim(a)) <> 0 Then fil.Name = a End Select File1.Refresh File2.Refresh Exit Sub ' 退出程序,以避免进入错误处理程序。 ErrorHandler: ' 错误处理程序。 MsgBox "应该选择一个文件", , "文件管理器" End Sub

  35. 【例9-8】将例9-2中的文本编辑器改为使用FSO对象模型。利用TextStream对象的属性和方法进行文本文件的各种操作。【例9-8】将例9-2中的文本编辑器改为使用FSO对象模型。利用TextStream对象的属性和方法进行文本文件的各种操作。 只需修改程序代码如下。 首先在通用过程中增加声明对象变量的代码: Dim fso As New FileSystemObject, fil As TextStream 然后修改按钮数组Command1的Click事件代码: Private Sub Command1_Click(Index As Integer) n = Index Select Case n Case 0 Text1.Text = "" Form1.Caption = “未命名” Case 1 CommonDialog1.ShowOpen '显示“打开”公共对话框 fname = CommonDialog1.FileName

  36. If fname <> "" Then Text1.Text = "" Set fil = fso.OpenTextFile(fname) b = "" b = fil.ReadAll Text1.Text = Left(b, 20000) End If Form1.Caption = fname Case 2 If Form1.Caption = "未命名" Or Form1.Caption = "" Then CommonDialog1.ShowSave '显示“另存为”公共对话框 fname = CommonDialog1.FileName Else fname = Form1.Caption End If

  37. If fname <> "" Then Set fil = fso.CreateTextFile(fname, True) fil.Write Text1.Text Form1.Caption = fname End If Case 3 CommonDialog1.ShowSave '显示“另存为”公共对话框 fname = CommonDialog1.FileName If fname <> "" Then Set fil = fso.CreateTextFile(fname, True) fil.Write Text1.Text Form1.Caption = fname End If

  38. Case 4 Text1.Text = "" End End Select Text1.SetFocus End Sub 习题九 9.1 随机文件与顺序文件读写过程的区别是什么? 9.2 使用Output选项打开一个已存在的文件会发生什么情况? 9.3 使用Append选项写文件会发生什么情况? 9.4 通过键盘输入数据,将包括学号、姓名、性别、数学、英语、电子学等学生成绩数据输入到一个顺序文件中(Stu.Dat)。 9.5 从Stu.Dat中读入全部学生成绩数据,将其中需要补考的学生数据存入一个新文件(Stu0.Dat)中。

  39. 9.6 从Stu0.Dat中读入数据,分别在列表框中显示单科需要补考的学生名单。 9.7 从Stu.Dat中读入全部学生成绩数据,将其中获奖学金的学生数据存入一个新文件(Stu1.dat)中。评奖学金的条件是:每门课程成绩均在85分以上或3门课程总分在270分以上。 9.8 从Stu1.Dat中读入数据,在列表框中显示获奖学金学生的各科成绩。 9.9 编写应用程序,功能如下: (1) 建立一个随机文件,管理某单位的职工情况。其中每个记录由工作证号、姓名、性别、工资、工作日期组成,可以向此文件添加新记录。 (2) 可以修改、删除记录。 (3) 可以按记录浏览所有职工的情况。 (4) 可以按姓名查找,并显示找到的记录。 (5) 可以按工作证号查找,并显示找到的记录。

  40. 9.10 创建简单的文本编辑器,其功能要求如下: (1) 可以通过“文件系统控件”选择文件的路径,也可以输入文件的路径。 (2) 可以读取所选择的文件,也可以将文本框中编辑的文件保存到磁盘上。 如图9-14所示。 9.11 在上题中使用文件系统对象,使得当目录不存在时可以创建一个新的目录。

More Related