快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

FILESYSTEMOBJECT组件新建、读取、添加、修改、删除功能实例

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>FileSystemObject组件应该实例</title>
</head>

<body>
<input name="B0" type="button" value="创建文本文件" onClick="window.location=\\\'test1.asp?type=crea\\\'">
<input name="B1" type="button" value="读取文件内容" onClick="window.location=\\\'test1.asp?type=read\\\'">
<input name="B2" type="button" value="添加文件内容" onClick="window.location=\\\'test1.asp?type=add\\\'">
<input name="B3" type="button" value="修改文件内容" onClick="window.location=\\\'test1.asp?type=edit\\\'">
<input name="B4" type="button" value="删除文件内容" onClick="window.location=\\\'test1.asp?type=dele\\\'">
<%
read="michael.txt"
\\\'获取文件真实路径
read=LEFT(Server.MapPath(Request.ServerVariables("PATH_INFO")),InstrRev(Server.MapPath(Request.ServerVariables("PATH_INFO")),"")) & read
\\\'response.write "文件路径:" & read & "<br>" \\\'输出文件真实路径
%>
<hr>
<%if request.querystring("type")="crea" then%>
<form action="test1.asp?type=crea" method="post" name="form1">
请输入文件名 : <input name="T1" size="45">.txt<br>
请输入文件内容:<textarea name="D1" cols="50" rows="5"></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%end if%>
<%if request.querystring("type")="add" then%>
<form action="test1.asp?type=add" method="post" name="form1">
<textarea name="D1" cols="70" rows="5"></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%end if%>
<%
if request.querystring("type")="edit" then
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then
Set ts=fs.OpenTextFile(server.mappath("michael.txt"))
%>
<form action="test1.asp?type=add" method="post" name="form1">
<textarea name="D1" cols="70" rows="5"><%=ts.ReadLine%></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%
else
response.write "找不到此文件"
end if
end if
%>
<%
if request.querystring("type")="crea" and request.form<>"" then \\\'创建新的文件
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath(request.form("T1")& ".txt")) then \\\'判定文件是否存在
response.write "<script>alert(\\\'该文件已经存在\\\');window.location=\\\'test1.asp?type=crea\\\';</script>"
else
Set ts=fs.CreateTextFile(server.mappath(request.form("T1")& ".txt"),True)
ts.close
Set ts=fs.OpenTextFile(server.mappath(request.form("T1")& ".txt"),2,True)
ts.WriteLine(request.form("D1")) \\\'写入文件内容 注:WriteLine是加入内容并换行,Write是加入内容不换行
ts.close
response.write "<script>alert(\\\'创建文件成功\\\');window.location=\\\'test1.asp?type=read\\\';</script>"

end if
set fs=nothing
end if

if request.querystring("type")="read" then \\\'读取文件内容
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then \\\'判定文件是否存在
Set ts=fs.OpenTextFile(server.mappath("michael.txt"))
response.write "文件内容: "
do until ts.AtEndOfStream \\\'判定是否读到文件最后一行
response.write ts.readLine & "<br>" \\\'读取文件逐行输出
loop
ts.close
else
response.write "找不到此文件"
end if
set fs=nothing
end if

if request.querystring("type")="add" and request.form<>"" then \\\'添加文件内容
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then \\\'判定文件是否存在
Set ts=fs.OpenTextFile(server.mappath("michael.txt"),8,True)
ts.WriteLine(request.form("D1")) \\\'写入文件内容 注:WriteLine是加入内容并换行,Write是加入内容不换行
ts.close
response.write "<script>alert(\\\'加入内容成功\\\');window.location=\\\'test1.asp?type=read\\\';</script>"
else
response.write "找不到此文件"
end if
set fs=nothing
end if

if request.querystring("type")="edit" and request.form<>"" then \\\'修改文件内容
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then \\\'判定文件是否存在
Set ts=fs.OpenTextFile(server.mappath("michael.txt"),2,True)
ts.WriteLine(request.form("D1")) \\\'写入文件内容 注:WriteLine是加入内容并换行,Write是加入内容不换行
ts.close
response.write "<script>alert(\\\'修改内容成功\\\');window.location=\\\'test1.asp?type=read\\\';</script>"
else
response.write "找不到此文件"
end if
set fs=nothing
end if

if request.querystring("type")="dele" then \\\'删除文件
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then \\\'判定文件是否存在
fs.DeleteFile server.mappath("michael.txt"),True
response.write "<script>alert(\\\'文件删除成功\\\');window.location=\\\'test1.asp\\\';</script>"
else
response.write "找不到此文件"
end if
set fs=nothing

end if
%>

</body>
</html>
返回类别: 教程
上一教程: 用ASP做ACCESS的远程接口
下一教程: SQL语言迅速入门之一

您可以阅读与"FILESYSTEMOBJECT组件新建、读取、添加、修改、删除功能实例"相关的教程:
· ASP直接读取修改ACCESS数据的演示实例
· 显示SQL数据库所有表的名称(带删除功能)
· 用ASP程序显示SQL数据库所有表的名称(带删除功能)
· 一个用组件动态创建EXCEL文件的实例
· 组件对象开发WEB应用的实例分析
    微笑服务 优质保证 索取样品