以下部份引用自 Visual Studio 6 MSDN 光碟
OpenTextFile 方法
描述
開啟一個指定的檔案並傳回一個 TextStream 物件,該物件可用於對檔案進行讀取或附加寫入。
準備工作
1. 進入VB6, 點選 MENU / Project / References
2. 點選 Microsoft Scripting Runtime, 並確定引用此 .dll 檔
語法
object.OpenTextFile(filename[, iomode[, create[, format]]])
OpenTextFile 方法有如下幾個單元:
單元 | 描述 |
Object | 必要引數。始終是一個 FileSystemObject 的名字。 |
filename | 必要引數。字串運算式,它標識了開啟的檔案。 |
iomode | 選擇性引數。表示輸入/輸出模式。可為兩個常數之一:ForReading或ForAppending。 |
create | 選擇性引數。Boolean 值,它表示如果指定的 filename 不存在是否可以建立一個開新檔案。如果建立開新檔案,其值為 True。若不建立檔案其值為 False。預設值為 False。 |
format | 選擇性引數。三種 Tristate 值之一,用於指定開啟檔案的格式。如果省略,則檔案以 ASCII 格式開啟。 |
設定
iomode 引數可為下面設定值的任何一個:
常數 | 描述 | |
ForReading | 1 | 開啟一個唯讀檔案。無法對此檔案進行寫入。 |
ForAppending | 8 | 開啟一個檔案並寫輸出至檔案的尾部。 |
Format 引數可為下面設定值的任何值:
常數 | 描述 | |
TristateUseDefault | –2 | 使用系統預設開啟檔案。 |
TristateTrue | –1 | 以 Unicode 格式開啟檔案。 |
TristateFalse | 0 | 以 ASCII 格式開啟檔案。 |
請注意
範例1:
下面的程式碼舉例說明了使用 OpenTextFile 方法開啟並讀取一個文字檔:
Sub OpenTextFileTest
Const ForReading = 1
Dim fso as FileSystemObject
Dim fid as TextStream
Set fso = CreateObject("Scripting.FileSystemObject")
Set fid = fso.OpenTextFile("c:\testfile.txt", ForReading)
MsgBox fid.ReadLine
fid.Close
End Sub
範例2:
下面的程式碼舉例說明了使用 OpenTextFile 方法開啟並附加(Append)文字於該文字檔:
Sub OpenTextFileTest
Const ForAppending = 8
Dim fso as FileSystemObject
Dim fid as TextStream
Set fso = CreateObject("Scripting.FileSystemObject")
Set fid = fso.OpenTextFile("c:\testfile.txt", ForAppending)
fid.WriteLine "This is a new line!"
fid.Close
End Sub
http://blog.xuite.net/hwtang/notes/23618734-VB%E8%AE%80%E5%8F%96%E6%96%87%E5%AD%97%E6%AA%94%E7%9A%84%E6%96%B9%E6%B3%95%28%E4%BD%BF%E7%94%A8FileSystemObject%29
全站熱搜
留言列表