最簡單的方法是使用filesystemobject對象。而它並非VB內建對象,
需引用才可以使用。
1.filesystemObject對象引用
“工程/引用/microsoft scription Runtime”
在物件瀏覽器視窗中選“scripting”模組,看到增了許多個物件,
drive filesystemobject textstream file等
其中filesystemobject是這些對象的關鍵,想要使用其它對象,
都必須先建立filesystemobject對象。
2.檔案存取
dim fs as new filesystemobject 建立filesystemobject對象
dim txtf as textstream 定義了一個textstream物件變數
textstrem對象與檔案的讀寫所有檔案都必須先開啟,才能讀寫,
filesystemobjet對象所提供的開啟檔案的方法有兩種:
opentextfile 開啟檔案
createtextfile 建立檔案
通過filesystemobject所開啟的檔案是一個TextStream對象,
而通過textstream對象的各種方法和屬性,就可以進一步讀寫檔案。
1)opentextfile 方法:開啟檔案
set textstream對象名=filesystemobject名.opentextfile(檔案名稱,IO模式,是否自動建立檔案)
檔案名稱:建議寫入完整路徑的檔案名稱。
IO模式:可設定成forreading(=1)、forwrting(=2)或forappending(=8)。
如果設定成forreading,則開啟的檔案是唯讀;
如果設定成forwriting,則開啟的檔案是可寫的,而原來檔案 的內容會被清除;
如果設定成forappending,則開啟的檔案是可寫的,但原檔案 的內容不會被清除,資料會從檔案 的最後面開始寫入。
參數預設: 表示forreading 唯讀。
是否自動建立檔案:可設定成true或false。
設定為:true 則當檔案不存在時,會自動 建立一個新檔案。
設定為:false 則當檔案不存在時,就會產生錯誤。
預設時:表示為false。
例:將c:/autoexec.bat開啟成為唯讀檔案
dim fs as new filesystemobject
dim txtf as textstream
set txtf=fs.opentextfile(“c:/autoexec.bat”)
例:將c:/text.txt開啟成“從檔案最後面寫入的檔案”
dim fs as new filesystemobject
dim txtf as textstream
set txtf=fs.opentextfile(“c:/test.txt”,forappending,true)
txtf.close
2)createtextfile 方法:建立檔案
set textstream對象名=filesystemobject名.createtextfile(檔案名稱,是 否覆蓋原檔案)
檔案名稱:建議寫入完整路徑的檔案名稱。
是否覆蓋原檔案:可設定成true或false。可預設,預設時是true,覆蓋原檔案
如果設定成true,則當檔案存在的時候,原文將會被破壞,而以新檔案所替代。
如果設定成false,則當檔案存在時,將會產生錯誤
例:建立一個c:/text.txt,若c:/test.txt存在,則覆蓋它。
dim fs as new filesystemobject
dim txtf as textstream
set txtf=fs.createtextfile(“c:/text.txt”)
3)fileexists 方法:檔案是否存在?
為了避免破壞已有的檔案,通常會將“是否覆蓋原檔案”參數設定為false。但是有可能產生錯誤,如何避免錯誤,可以先判斷檔案是否存在,此時調用filesystemobjet 對象的fileexists方法,具體程式如下:
‘fs 為filesystemobject
if fs.fileexists(“c:/test.txt”) then ‘檔案已存在
‘其它處理方式
else
set txtf=fs.createtextfile(“c:/text.txt”)
end if
4)readline 和writeline方法:檔案的讀取與寫入
利用createdtextfile和opentextfile所開啟(建立)的是文字檔,要存取文字檔,通常採用逐行讀取或逐行寫入的方式。讀取時必須調用textstream對象的readline方法。寫入時必須調用writeline方法。
例:
‘txtf 和txtf2均為textstream對象
s=txtf.readline ‘讀取一行資料,並設定給s變數
txtf2.writeline s ‘將變數s的資料作為檔案一行資料寫入
5)atendofstream 屬性:檔案是否已到末尾?
當讀取位置已經到達檔案的末尾時,如果再調用readlin讀取資料,將會產生錯誤,為了避免這個錯誤,必須判斷textstream對象的atendofstream屬性,如果為true,就表示檔案位置已經到達檔案末尾,不可以再讀資料。
If not txtf.atendofstream then
s=txtf.readline
endif
例:將c:/autoexec.bat檔案複製成為c:/autoexec.bak檔案
dim fs as new filesystemobject
dim txtf1 as textstream
dim txtf2 as textstream
dim s as string
set txtf1=fs.opentextfile(“c:/autoexec.bat”,forreading,true)
set txtf2=fs.createtextfile(“c:/autoexec.bak”,true)
while not txtf1.atendofstream ‘如果txtf1還沒有到達檔案末尾
s=txtf1.readline ‘從textf1中讀取一行資料
txtf2.writeline s ‘將資料作為txtf2的一行資料寫入到檔案
wend
txtf1.close
txtf2.close
6)readall 方法:一次讀取檔案的所有內容
readline方法每次唯讀取一行資料,readall方法則是一次讀取檔案的所有內容。
‘txtf 為textstream對象
if not txtf.atendofstream then
text1.text =txtf.readall
end if
end sub
7)read和write方法:檔案的讀取和寫入
read與readline方法最大的不同在於readline每次會讀取一行資料,但是read方法則是每次讀取N個字元,例如:
n=100
s=txtf.read (n) ‘讀取100個字元
如果檔案中的字元數不足100,假設只乘下50個字元,那read就會將50個字元全部讀取出來。除了讀取方式的不同之外,readline只能用來讀取文體檔案,read方法還可以用來讀取二進位檔案。(例:.ext 、dll、bmp等格式檔案)
write與writeline方法最大的不同之處在於writeline每次向檔案寫入資料的時候,都會再多寫入chr(13)和chr(10)兩個字元,但write則是只是單純地寫入資料。
例:利用readall和write方法將c:/autoexec.bat檔案複製成為c:/autoexec.bak檔案。
Dim fs as new filesystemobject
dim txtf as textstream, txtf2 as textstream
set txtf=fs.opentextfile(“c:/autoexec.bat”,forreading,true)
set txtf2=fs.createtextfile(“c:/autoexec.bak”,true)
txtf2.write txtf.readall
txtf2.close
txtf.close
首先:定義一個全域變數textchange 記載文字框是否曾修改過。
2 在new_Click()事件中
Private Sub new_Click()
If textchange = 1 Then
aa = MsgBox("是否儲存已修改過的檔案", vbOKCancel)
If aa = 1 Then
savefile ‘使用者定義過程,作用儲存檔案
End If
End If
Text1.Text = ""
Text1.SetFocus
textchange = 0
End Sub
Savefile過程:
Private Sub savefile()
Dim fs As New FileSystemObject
Dim txtf As TextStream
CommonDialog1.ShowSave
Set txtf = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, True)
txtf.Write Text1.Text
txtf.Close
textchang = 0
End Sub
3 在open_Click()事件中
Private Sub open_Click()
Dim fs As New FileSystemObject
Dim txtf As TextStream
If textchange = 1 Then
aa = MsgBox("是否儲存已有的修改?", vbOKCancel)
If aa = 1 Then
savefile
End If
End If
Text1.Text = ""
textchange = 0
CommonDialog1.ShowOpen
Set txtf = fs.OpenTextFile(CommonDialog1.FileName, ForReading, True)
Text1.Text = txtf.ReadAll
txtf.Close
End Sub
Private Sub save_Click()
savefile
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "open"
open_Click
Case "new"
new_Click
Case "save"
save_Click
Case "copy"
mcopy_Click
Case "cut"
mcut_Click
Case "paste"
mpaste_Click
End Select
End Sub