使用 asp.net 和 C# 列出目录中的文件夹 [英] Listing Folders in a Directory using asp.net and C#
本文介绍了使用 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);
}
}
我想列出特定目录中的文件夹,但它一直显示空白页.谁能告诉我代码中有什么问题.
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.
推荐答案
在空白页上显示目录和文件
Display directories and files on a blank page
// YourPage.aspx
<%@ Import Namespace="System.IO" %>
<html>
<body>
<% foreach (var dir in new DirectoryInfo("E:\TEMP").GetDirectories()) { %>
Directory: <%= dir.Name %><br />
<% foreach (var file in dir.GetFiles()) { %>
<%= file.Name %><br />
<% } %>
<br />
<% } %>
</body>
</html>
这篇关于使用 asp.net 和 C# 列出目录中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文