我有这个问题,我似乎无法克服。我正在尝试列出所有目录和子目录。这是我目前在代码中所拥有的:
String[] Folders;
String[] Files;
path = Server.MapPath("/");
DirectoryInfo dir = new DirectoryInfo(path);
Folders = Directory.GetDirectories(path);
try
{
FolderListing.Append("<ul id=\"FolderList\">");
for (int x = 0; x < Folders.Length; x++ )
{
DirectoryInfo folder = new DirectoryInfo(Folders[x]);
FolderListing.Append("<li>").Append(folder.Name).Append("</li>");
CheckSubdirectories(Folders[x]);
}
FolderListing.Append("</ul>");
FolderList.Text = FolderListing.ToString();
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
private void CheckSubdirectories(string currentpath)
{
String[] subfolders = Directory.GetDirectories(currentpath);
if (subfolders.Length != 0)
{
FolderListing.Append("<ul id=\"SubFolderList\">");
}
for (int x = 0; x < subfolders.Length; x++ )
{
DirectoryInfo sfolder = new DirectoryInfo(subfolders[x]);
FolderListing.Append("<li>").Append(sfolder.Name).Append("</li>");
}
if (subfolders.Length != 0)
{
FolderListing.Append("</ul>");
}
path = currentpath.ToString();
}
我希望最终结果是:
<ul>
<li>admin</li>
<ul>
<li>Containers</li>
<li>ControlPanel</li>
<li>Menus</li>
<ul>
<li>etc etc</li>
</ul>
</ul>
</ul>
如果有人可以帮助我,请
答案 0 :(得分:1)
这有效:
Func<DirectoryInfo, XElement[]> getDirectories = null;
getDirectories = di =>
(new []
{
new XElement("li", di.Name),
di.GetDirectories().Any()
? new XElement("ul",
from cdi in di.GetDirectories()
select getDirectories(cdi))
: null,
})
.Where(x => x != null)
.ToArray();
var xml = new XElement("ul",
getDirectories(
new DirectoryInfo(@"E:\Install\_.NET")));
从我的示例目录中我得到了这个输出:
<ul>
<li>_.NET</li>
<ul>
<li>3DCollaborator</li>
<li>MercurialVSSProvider</li>
<li>MongoDB</li>
<li>sqlite</li>
<ul>
<li>sqlite-netFx35-binary-bundle-Win32-2008-1.0.74.0</li>
<li>sqlite-netFx35-binary-Win32-2008-1.0.74.0</li>
<li>sqlite-netFx40-binary-Win32-2010-1.0.74.0</li>
</ul>
</ul>
</ul>
答案 1 :(得分:0)
您可以使用递归方法解决问题。您需要一个调用自身的方法,以列出嵌套在任何深度的子目录。
public ListDirectory (DirectoryInfo dir)
{
//TODO: Output the directory info here.
foreach (DirectoryInfo subdir in dir.Directories) {
ListDirectory(subdir);
}
foreach (FileInfo file in dir.Files) {
//TODO: Output the file info here.
}
}
列表在HTML中应如下所示。请注意,子目录<li>
必须包含整个嵌套<ul>
。
<ul>
<li>subdirectory item 1
<ul>
<li>file 1 in subdirectory</li>
<li>file 2 in subdirectory</li>
</ul>
</li>
<li>subdirectory item 2
<ul>
<li>sub-subdirectory item
<ul>
<li>file 1 in sub-subdirectory</li>
<li>file 2 in sub-subdirectory</li>
</ul>
</li>
<li>file 1 in subdirectory</li>
<li>file 2 in subdirectory</li>
</ul>
</li>
<li>file item 1</li>
<li>file item 2</li>
</ul>
这将产生以下输出:
有了这些信息,我们可以改进上面的方法(伪代码)
public ListDirectory (DirectoryInfo dir)
{
Output("<li>"); Output(dir.Name);
if (dir.Directories.Count > 0 || dir.Files.Count > 0) {
Output("<ul>");
foreach (DirectoryInfo subdir in dir.Directories) {
ListDirectory(subdir);
}
foreach (FileInfo file in dir.Files) {
Output("<li>"); Output(file.Name); Output("</li>");
}
Output("</ul>");
}
Output("</li>");
}
您还必须将整个事件包含在<ul> </ul>
对中。此外,您可以在第一级使用稍微不同的方案,以便列出文件和目录而不是一个根目录,就像上面的代码一样。
答案 2 :(得分:0)
希望这种递归方法有所帮助:
void ListDirectories(string path, StringBuilder sb)
{
var directories = Directory.GetDirectories(path);
if (directories.Any())
{
sb.AppendLine("<ul>");
foreach (var directory in directories)
{
var di = new DirectoryInfo(directory);
sb.AppendFormat("<li>{0}</li>", di.Name);
sb.AppendLine();
ListDirectories(directory, sb);
}
sb.AppendLine("</ul>");
}
}
要调用它,只需创建StringBuilder
的实例并将其与方法的路径一起发送:
var sb = new StringBuilder();
ListDirectories(Server.MapPath("/"), sb);