使用 asp.net 和 C# 列出目录中的文件夹
.aspx 文件:
<%@ Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Explorer</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
.CS 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class view2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = "~/";
GetFilesFromDirectory(path);
}
private static void GetFilesFromDirectory(string DirPath)
{
try
{
DirectoryInfo Dir = new DirectoryInfo(DirPath);
FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo FI in FileList)
{
Console.WriteLine(FI.FullName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
我想列出特定目录中的文件夹,但它不断显示空白页。任何人都可以告诉代码中的问题是什么。
.aspx file:
<%@ Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Explorer</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
.CS file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class view2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = "~/";
GetFilesFromDirectory(path);
}
private static void GetFilesFromDirectory(string DirPath)
{
try
{
DirectoryInfo Dir = new DirectoryInfo(DirPath);
FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo FI in FileList)
{
Console.WriteLine(FI.FullName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I want to list the folders in a particular directory but it continuously showing blank page.Can anybody tell what's the problem in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在空白页上显示目录和文件
Display directories and files on a blank page
不要使用
Console.WriteLine()
,而使用Response.Write()
。您正在尝试写入 Web 应用程序中的控制台。Don't use
Console.WriteLine()
useResponse.Write()
. You're trying to write to the console in a web application.Console.WriteLine 将写入控制台,而不是您返回的网页内容。您需要将一个容器元素添加到您的 ASPX 页面,可能是一个网格视图或转发器,然后从代码隐藏文件中添加分配文件列表(对于您添加的 HTML 元素,使用 runat='server' 标记并为其分配一个ID,然后在代码中通过ID名称引用它)。
Console.WriteLine will write to the console, not the web page contents you are returning. You need to add a container element to your ASPX page, probably a grid view or repeater, then add assign the file list from the code behind file (to the HTML element you added, use the runat='server' tag and assign it an ID, then reference it by ID name in the code).
Response.Write 在静态代码隐藏方法中:脏!另外你也没有控制你写的位置。这个比较干净一点...
Response.Write in a static codebehind method: DIRTY! In addition you did't control the position where you write. This a little bit cleaner...
您可以使用 Directory 类
/
在aspx页面中
You can use Directory class
/
In the aspx page