ASP.NET 2.0 C# Generics List Collection Folder Control
2007-05-20 23:25
537 查看
This page demonstrates using a Generics List Collection in C# how to retrieve all folders, subfolder, and folders within subfolders.
/**//*
In this example, we are getting all folders, subfolders, and folders within
subfolders. Then we can load them into any control.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
namespace StrongTypes
...{
public class FolderExample
...{
private List<string> ListFolders = new List<string>();
public void PopulateFolders(string strFolderLocation, Control ctrl)
...{
LoadFolders(strFolderLocation);
foreach (string str in ListFolders)
...{
//Add code here to add folders and subfolders to Control
}
}
private void LoadFolders(string strFolderLocation)
...{
DirectoryInfo dir = null;
try
...{
dir = new DirectoryInfo(strFolderLocation);
FileSystemInfo[] info = dir.GetFileSystemInfos();
foreach (FileSystemInfo fsi in info)
...{
if (fsi is DirectoryInfo)
...{
ListFolders.Add(fsi.FullName);
}
AddDirectoriesToList(info);
}
}
catch
...{
if (strFolderLocation == null)
...{
throw new NullReferenceException("The property FileLocation cannot be null");
}
if (!(dir.Exists))
...{
throw new DirectoryNotFoundException("The directory you are trying to search does not exist.");
}
}
}
private void AddDirectoriesToList(FileSystemInfo[] info)
...{
foreach (FileSystemInfo fsi in info)
...{
if (fsi is DirectoryInfo)
...{
if (!(ListFolders.Contains(fsi.FullName)))
...{
ListFolders.Add(fsi.FullName);
}
DirectoryInfo dir = (DirectoryInfo)fsi;
AddDirectoriesToList(dir.GetFileSystemInfos());
}
}
}
}
}
/**//*
In this example, we are getting all folders, subfolders, and folders within
subfolders. Then we can load them into any control.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
namespace StrongTypes
...{
public class FolderExample
...{
private List<string> ListFolders = new List<string>();
public void PopulateFolders(string strFolderLocation, Control ctrl)
...{
LoadFolders(strFolderLocation);
foreach (string str in ListFolders)
...{
//Add code here to add folders and subfolders to Control
}
}
private void LoadFolders(string strFolderLocation)
...{
DirectoryInfo dir = null;
try
...{
dir = new DirectoryInfo(strFolderLocation);
FileSystemInfo[] info = dir.GetFileSystemInfos();
foreach (FileSystemInfo fsi in info)
...{
if (fsi is DirectoryInfo)
...{
ListFolders.Add(fsi.FullName);
}
AddDirectoriesToList(info);
}
}
catch
...{
if (strFolderLocation == null)
...{
throw new NullReferenceException("The property FileLocation cannot be null");
}
if (!(dir.Exists))
...{
throw new DirectoryNotFoundException("The directory you are trying to search does not exist.");
}
}
}
private void AddDirectoriesToList(FileSystemInfo[] info)
...{
foreach (FileSystemInfo fsi in info)
...{
if (fsi is DirectoryInfo)
...{
if (!(ListFolders.Contains(fsi.FullName)))
...{
ListFolders.Add(fsi.FullName);
}
DirectoryInfo dir = (DirectoryInfo)fsi;
AddDirectoriesToList(dir.GetFileSystemInfos());
}
}
}
}
}
相关文章推荐
- Asp.net(C#/2.0) 部分全选 CheckBoxList
- [转] 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(二)
- CSS Friendly ASP.NET 2.0 Control Adapters - CSS Optimized Markup for ASP.NET Controls
- .C#中如何给时间变量赋一个空值ASP.NET 2.0
- 获取汉字第一个拼音 c# asp.net 2.0
- ASP.NET 2.0(C#)实现多文件上传
- Teddy's Knowledge Base--基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V2.0.0 [开源]
- 清华大学出版社《ASP.NET第一步—基于C#和ASP.NET 2.0》试读&出版
- ASP.NET 2.0网站专案同时使C#与VB.NET之技巧(新书连载)
- Inside ASP.NET 2.0 DataBound Control - 1
- 正则表达式 常用收藏(34例) - ASP.NET 2.0(C#)
- Access 通用数据访问类(asp.net 2.0 c#)
- ASP.net(c#)取得CheckBoxList(复选框)的值
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- (原)ASP.NET 2.0(C#)实现多文件上传
- Asp.net 2.0 C#实现压缩/解压功能 [转=向作者‘肖相’无私共享精神致敬]
- [翻译]Accessing and Updating Data in ASP.NET 2.0: Examining the Data Source Control's Events
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码)