So, I was facing the task for displaying the directory and file contents for each directory and could not find any good help... anyways, here are the functions as I came up with them
Displaying all the directory names along with date last modified
private string displayDirectories( DirectoryInfo parentDirectory )
{
DirectoryInfo[] diArrGE = parentDirectory.GetDirectories();
// Response.Write(parentDirectory.Name + "
");
String link = "
";
foreach (DirectoryInfo dri in diArrGE)
{
Int32 index = dri.Name.IndexOf('_');
String dirName = dri.Name.Substring(index).Replace("_", " ");
link += string.Format("
", category, dri.Name, dirName, dri.LastWriteTime.ToLongDateString());
}
link += "
";
return link;
}
Displaying all the files in a directory sorted by date modified...
private string displayFiles( DirectoryInfo di )
{
string link = string.Empty;
StringBuilder test = new StringBuilder();
//I just need html files in the directory
FileInfo[] rgFiles = di.GetFiles("*.html");
try
{
string category = "";
if (di.FullName.Contains("Agenda"))
{
category = "Agenda";
}
else if (di.FullName.Contains("Minutes"))
{
category = "Minutes";
}
else
{
category = "Panel";
}
// Now read the creation time for each file
DateTime[] creationTimes = new DateTime[rgFiles.Length];
for (int i = 0; i < rgFiles.Length; i++)
creationTimes[i] = rgFiles[i].LastWriteTime;
// Response.Write(creationTimes.Length);
// sort it
Array.Sort(creationTimes, rgFiles);
foreach (FileInfo fi in rgFiles)
{
string[] result = fi.Name.Split(new Char[] { '.' });
string[] fileName = result[0].Split('_');
string fileNameDisplay = string.Format("{0} {1},{2}", fileName[1], fileName[0], fileName[2]);
// Response.Write(fileNameDisplay);
link = "
" + fileNameDisplay + " {Last Update: " +
fi.LastWriteTime.ToString("D") + "}";
test.Insert(0, link);
}
//link = "";
test.Insert(0, "
");
test.Append("
");
}
catch (Exception e)
{
Response.Write("Error in file " + e);
}
return test.ToString();
}
I really liked the sorting function
// Now read the creation time for each file
DateTime[] creationTimes = new DateTime[rgFiles.Length];
for (int i = 0; i < rgFiles.Length; i++)
creationTimes[i] = rgFiles[i].LastWriteTime;
// Response.Write(creationTimes.Length);
// sort it
Array.Sort(creationTimes, rgFiles);
WOO WOO for that