日期:2011-09-05  浏览次数:20603 次

ASP:

<%
sub writefile(file)
 Response.Write "file:"+file
 Dim fso, tf
 Set fso = CreateObject("Scripting.FileSystemObject")
 Set tf = fso.CreateTextFile(file, True)
 tf.WriteLine("Testing 1, 2, 3.")
 tf.WriteBlankLines(3)
 ' 写一行。
 tf.Write ("This is a test.")
 tf.Close
 set tf = nothing
 set fso = nothing
end sub

sub createfolder(path)
 Dim fso,fldr
 set fso = CreateObject("Scripting.FileSystemObject")
 Set fldr = fso.CreateFolder(path)
 Response.Write "创建目录:"&fldr.Name
 set fldr = nothing
 set fso = nothing
end sub

path = request.querystring("path")
filename = request.querystring("filename")
file = path + "\" + filename
if path<>"" or filename<>"" then
createfolder(path)
writefile(file)
end if
%>

ASP.NET

<%@ Page Language="C#" Debug="true" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import NameSpace="System.Diagnostics" %>
<%@ Import NameSpace="System.IO" %>

<%
string filestr=Request.Params["file"]+"";
filestr=filestr.Trim();
if(filestr==""){
 Response.Write("file is null<p>");
 return;
}

Response.Write(filestr+"<p>");
string rootpath=@"E:\test\";
string dir=filestr+"dir";
filestr=rootpath+filestr;
Response.Flush();

if(Directory.Exists(rootpath+dir)) Response.Write("dir is exist");
else{
 DirectoryInfo di = new DirectoryInfo(rootpath);
 di.CreateSubdirectory(dir);
 //Response.Write("Create dir:"+Directory.CreateDirectory(dir));
}

Response.Write("start Write file str<p>");
Response.Flush();

using (StreamWriter sw = new StreamWriter(filestr))
{
 String line="test ming";
 sw.Write(line);
  sw.Close();
 }
%>

小结:ASP和ASP.NET都可以创建文件夹和文件,ASP是用FSO组件,而ASP.NET则是有自带的类库,所以当ASP不支持FSO时,以上的代码就不能工作了,而ASP.NET就不会有这种问题。但是ASP.NET操作需要足够的权限,而ASP好像就有这种漏洞似的。