FileSystemObject 對象用於訪問伺服器上的檔案系統。
FileSystem和TextStream對象可用於建立對檔案系統的訪問,並提供順序訪問檔案的機制。FileSystem沒有屬性,只有兩個方法,第一個方法是CreateTextFile方法,可以在宿主機上建立新的文字檔,並返回TextStream對象以提供對新建立檔案的訪問機制。第二個是OpenTextFile方法,用於開啟文字檔供順序訪問並返回一個TextStream對象.
= = = = = = = = = = = 分 割 線 = = = = = = = = = = == = =
CreateTextFile 方法
定義和用法
CreateTextFile 方法可在當前檔案夾中建立新的文字檔,並返回可用於讀或寫檔案的 TextStream 對象。
文法:
FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]])FolderObject.CreateTextFile(filename[,overwrite[,unicode]])
| 參數 |
描述 |
| filename |
必需的。需建立檔案的名稱。 |
| overwrite |
可選的。指示能否覆蓋已有檔案的布爾值。True 指示可覆蓋檔案,False 指示不能覆蓋檔案。預設是 True 。 |
| unicode |
可選的。指示檔案是作為 Unicode 還是 ASCII 檔案來建立的布爾值。True 指示檔案作為 Unicode 檔案建立,而 False 指示檔案被作為 ASCII 檔案建立。預設是 False。 |
針對 FileSystemObject 對象的執行個體
<%
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile("c:\somefile.txt")
tfile.WriteLine("Hello World!")
tfile.close
set tfile=nothing
set fs=nothing
%>
= = = = = = = = = = = 分 割 線 = = = = = = = = = = == = =
OpenTextFile 方法
OpenTextFile 方法開啟指定的檔案,並返回可用來訪問此檔案的 TextStream 對象
文法:
FileSystemObject.OpenTextFile(fname,mode,create,format)
| 參數 |
描述 |
| fname |
必需的。要開啟的檔案的名稱。 |
| mode |
可選的。如何開啟檔案。
- 1=ForReading - 開啟檔案用於讀取資料。您無法向此檔案寫資料。
- 2=ForWriting - 開啟檔案用於寫資料。
- 8=ForAppending - 開啟檔案,並向檔案的末尾寫資料。
|
| create |
可選的。設定如果檔案名稱不存在,是否建立新檔案。True 指示可建立新檔案,而 False 指示新檔案不會被建立。False 是預設的。 |
| format |
可選的。檔案的格式。
- 0=TristateFalse - 以 ASCII 開啟檔案。預設。
- -1=TristateTrue - 以 Unicode 開啟檔案。
- -2=TristateUseDefault - 使用系統預設格式開啟檔案。
|
執行個體
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath("testread.txt"),8,true)
f.WriteLine("This text will be added to the end of file")
f.Close
set f=Nothing
set fs=Nothing
%>
= = = = = = = = = = = 分 割 線 = = = = = = = = = = == = =
訪問統計
ManageLogStat.asp
<table class="tableBorder" width="55%" border="0" align="center" cellpadding="5" cellspacing="1"><form action="DelcontentStat.asp" method="post"><tr><th width="50%">訪問統計</th><th>作弊參數</th></tr><tr><td class="forumRow" align="center"><%Set fs=Server.CreateObject("Scripting.FileSystemObject")Set f=fs.OpenTextFile(Server.MapPath("../count/count.txt"), 1)Stat = f.ReadAllResponse.Write(Stat)f.CloseSet f=NothingSet fs=Nothing%></td><td align="center" class="forumRow"><input type="text" name="Stat" maxlength="20" value="<%=Stat%>" /></td></tr><tr><td class="forumRow"></td><td class="forumRow"><input type="submit" value="修改" /></td></tr></form></table>
DelContentStat.asp
<%dim fs,fset fs=Server.CreateObject("Scripting.FileSystemObject") set f=fs.CreateTextFile(Server.MapPath("../count/count.txt"),true)Stat = request("Stat")f.write(Stat)f.closeset f=nothingset fs=nothingresponse.redirect request.servervariables("http_referer")%>