文件管理:
语句管理:打开、open----for output(append)===写操作
Input-----write(print)通过input进行读操作
Close---关闭文件
FSO----file system object 文件系统的简称
将复杂语句转化成对象的方法
对象种类:
通过 ----工程-----引用:Microsoft script runtime
Fso 主对象 文本流对象---与内容有关的(读写、新建)必须创建文本流对象
Drive 驱动器对象:用来收集驱动器信息,如可用磁盘空间或驱动器类型
Folder 文件夹对象:用于创建、删除、或移动文件夹
File 文件对象:用于创建、删除、或移动文件
Textstream对象:用来完成对文本文件的读写操作
Filesystemobject对象:是fso对象模型的主对象
创建(声明)一个fso对象
1. dim fso1 as new filesystemobject
2. dim fso1 as filesysytemobject
set fso1=new filesystemobject
3. set fao1=createobject(“scriptring.filesyatemobject”)
创建(声明)一个textstream对象
新文本流—fso1.createtextfile
打开----fso1.opentextfile
File.openastextstream
实例:用fso对象新建、打开、保存文件
Dim fso As New FileSystemObject
Dim modi As Boolean
Dim new1 As Boolean
Dim f
Dim x As Integer
Dim filename As String
Private Sub menunew_Click()
new1 = True
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub menuopen_Click()
If modi Then
x = MsgBox("数据已经被修改,你确定要保存吗?", vbYesNo)
If x = vbNo Then Exit Sub
End If
Dim fso As New FileSystemObject
Dim f
Dim ts As TextStream '通过此对象可调用一些方法
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.Filter = "文本文件(*.txt) |*.txt| 所有文件 (*.*) | *.*"
CommonDialog1.ShowOpen
filename = CommonDialog1.filename
Set f = fso.GetFile(filename)
Set ts = f.OpenAsTextStream(ForReading)
Text1.Text = ts.ReadAll
ts.Close
Set ts = Nothing
Set f = Nothing
Set fso = Nothing
Text1.SetFocus
End Sub
Private Sub menusave_Click()
Dim fso, f
If modi And new1 Then
CommonDialog1.DialogTitle = "保存文件"
CommonDialog1.Filter = "文本文件(*.txt) |*.txt"
CommonDialog1.ShowSave
filename = CommonDialog1.filename
Set fso = CreateObject("scripting.filesystemobject")
fso.CreateTextFile filename
Set f = fso.GetFile(filename)
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write Text1.Text
ts.Close
modi = False
xinjian = False
End If
If modi = True Then
Set fso = CreateObject("scripting.filesystemobject")
fso.CreateTextFile filename
Set f = fso.GetFile(filename)
Set ts = f.OpenAsTextStream(ForWriting)
ts.Write Text1.Text '将text1.text写入到文件filename中
ts.Close
modi = False
End If
End Sub
Private Sub Text1_Change()
modi = True
'new1=True
End Sub