I am attempting to make a Custom site map provider for the first time, but I am stuck on the sub folders. When I try to add my files and folders I am not sure how to build the path for the Directory Info as it drops down a level in the folders.
Example:
It starts out as ~Content for the Root
~Content/folder for the folder
~Content/folder/file for the file
but when I get to a sub folder(s) I am not sure how to append the path to something like this:
~Content/folder/folder/folder/file as it loops through the sub-folders.
I appreciate any help as I am in over my head.
Loading
JeremyPosted Jun 2, 2011, 3:00 PM
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
///
/// Summary description for MyCustomSiteMap
///
public class MyCustomSiteMap : StaticSiteMapProvider
{
string sourceDir = HttpContext.Current.Server.MapPath(Path.Combine(HttpContext.Current.Request.ApplicationPath, "Content"));
private SiteMapNode parentNode
{
get;
set;
}
private string ExcludedFolders
{
get
{
return "(App_Data)|(obj)";
}
}
private string ExcludedFiles
{
get
{
return "";
}
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{
parentNode = HttpContext.Current.Cache["SiteMap"] as SiteMapNode;
if (parentNode == null)
{
base.Clear();
parentNode = new SiteMapNode(this,
sourceDir,
sourceDir + "Default.aspx",
"Home");
AddNode(parentNode);
AddFiles(parentNode);
AddFolders(parentNode);
HttpContext.Current.Cache.Insert("SiteMap", parentNode);
}
return parentNode;
}
}
private void AddFolders(SiteMapNode parentNode)
{
List
DirectoryInfo di = new DirectoryInfo(parentNode.Key);
foreach (DirectoryInfo dir in di.GetDirectories("*", SearchOption.AllDirectories))
{
subFolders.Add(dir.Name);
}
foreach (var fldrName in subFolders)
{
string folderUrl = parentNode.Key + "\\" + fldrName;
SiteMapNode folderNode = new SiteMapNode(this, folderUrl, null, fldrName);
AddNode(folderNode, parentNode);
AddFiles(folderNode);
}
}
private void AddFiles(SiteMapNode folderNode)
{
List
DirectoryInfo di = new DirectoryInfo(folderNode.Key);
foreach (FileInfo file in di.GetFiles("*.pdf"))
{
files.Add(file.Name);
}
foreach (var fileName in files)
{
SiteMapNode fileNode = new SiteMapNode(this,
folderNode.Key + "/" + fileName,
null,
fileName);
AddNode(fileNode, folderNode);
// AddFolders(parentNode);
}
}
protected override SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}
}