回 帖 发 新 帖 刷新版面

主题:ASP.NET中获取驱动器及列出目录文件的问题?急!!

listdrives.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO"%>
<%
string[] achdrives=Directory.GetLogicalDrives();
int nnumofdrives=achdrives.Length;
Response.Write("<ul>");
for(int i=0;i<nnumofdrives;i++)
{
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(achdrives[i]));
Response.Write("\">"+achdrives[i]);
Response.Write("</a><br>");
}
Response.Write("</ul>");
%>

listdir.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO"%>
<%
string strDir2list=Request.QueryString.Get("dir");
Directory thisOne=null;
try
{
thisOne=new Directory(strDir2list);
//Reading the directory properties
Response.Write("<p>Creation:"+thisOne.CreationTime.ToString()+"</p>");
Directory[] subdirectories=thisOne.GetDirectories();
Response.Write("<ul>");
for(int i=0;i<subdirectories.Length;i++)
{
Response.Write("<li><a href=\"listdir.aspx?dir=");
Response.Write(Server.UrlEncode(subdirectories[i].FullName));
Response.Write("\">"+subdirectories[i].Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");

File[] thefiles=thisOne.GetFiles();
Response.Write("<ul>");
for(int i=0;i<thefiles.Lenth;I++)
{
Response.Write("<li><a href=\"showfile.aspx?file=");
Response.Write(Seerver.UrlEncode(thefiles[i].FullName));
Response.Write("\">"+thefiles[i].Name);
Response.Write("</a><br>");
}
Response.Write("</ul>");
}
catch (Excepton e)
{
Response.Write("Access not possible, error:<i>");
Response.Write(e.ToString()+"</i>");
Response.End();
}
%>

在执行时,可以列出驱动器,但是列出目录就有问题了。
提示listdir.aspx中
try
行 7:  {
行 8:  thisOne=new Directory(strDir2list); 此行重载“Directory”方法未获取                      “1”参数

行 9: Response.Write("<p>Creation:"+thisOne.CreationTime.ToString()+"</p>");
请问那位朋友帮下忙指教一下,我也是刚开始学,希望大家多多帮忙啦!谢谢

回复列表 (共2个回复)

沙发

<% @Page Language="C#" %> 
<% @Import Namespace="System.IO" %> 
<% 
string strDir2List = Request.QueryString.Get("dir"); 
//Directory thisOne = null; 
try 

DirectoryInfo thisOne =new DirectoryInfo(strDir2List); 
// Reading the directory properties 
Response.Write("<p>Creation: " + thisOne.CreationTime.ToString() + "</p>"); 
DirectoryInfo [] subDirectories = thisOne.GetDirectories(); 
Response.Write("<ul>"); 
for (int i=0; i < subDirectories.Length; i++) 

Response.Write("<li><a href=\"listdir.aspx?dir="); 
Response.Write(Server.UrlEncode(subDirectories[i].FullName)); 
Response.Write("\">" + subDirectories[i].Name); 
Response.Write("</a><br>"); 

Response.Write("</ul>"); 
  
FileInfo[] theFiles = thisOne.GetFiles(); 
Response.Write("<ul>"); 
for (int i=0; i < theFiles.Length; i++) 

Response.Write("<li><a href=\"showfile.aspx?file="); 
Response.Write(Server.UrlEncode(theFiles[i].FullName)); 
Response.Write("\">" + theFiles[i].Name); 
Response.Write("</a><br>"); 

Response.Write("</ul>"); 

catch (Exception e) 

Response.Write("Access not possible, error: <i>"); 
Response.Write(e.ToString() + "</i>"); 
Response.End(); 

%> 

板凳

看你的错误报告后,我觉得是你的Directory()里面带的参数不对,检查下!^-^

我来回复

您尚未登录,请登录后再回复。点此登录或注册