Displaying Folder Contents as menu Items

It may be required to show list of files or folders of a given path as a menu.
The following classes encapsulate the retrieval of folder information


public class FolderList
{
FileItem[] arFolderItems, arFileItems;
public void GetFolders(string ParentFolderName)
{
string[] arFolders = Directory.GetDirectories(HttpContext.Current.Server.MapPath(ParentFolderName));
arFolderItems= new FileItem[arFolders.Length];

for (int i = 0; i < arFolderItems.Length; i++)
{
string folderAbsName = arFolders[i];
string folderName = folderAbsName.Substring(folderAbsName.LastIndexOf("\\") + 1);
arFolderItems[i] = new FileItem(folderName);
}
}

public void GetFiles(string ParentFolderName)
{
string[] arFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath(ParentFolderName));
arFileItems = new FileItem[arFiles.Length];
for (int i = 0; i < arFileItems.Length; i++)
{
string fileAbsName = arFiles[i];
string fileName = fileAbsName.Substring(fileAbsName.LastIndexOf("\\") + 1);
arFileItems[i] = new FileItem(fileName);
}
}
public FileItem[] Subfolders{ get { return arFolderItems; } }
public FileItem[] Files { get { return arFileItems; } }
}
public class FileItem
{
string text;
public string Text { get { return text; } }
public FileItem(string Text) { this.text = Text; }
}




To display the contents of the "Downloads" folder as a menu, use the following Code



protected void Page_Load(object sender, EventArgs e)
{
FolderList f = new FolderList();
f.GetFolders("Downloads");
Repeater1.DataSource = f.Subfolders;
Repeater1.DataBind();
}



Use the following code to show filenames in "Images" folder in a repeater control.



protected void Page_Load(object sender, EventArgs e)
{
FolderList f = new FolderList();
f.GetFiles("Images");
Repeater1.DataSource = f.Files;
Repeater1.DataBind();

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: