Adding subfolders in a directory to an arraylist, making the folder accessable for new arraylist of files in that subfolder. Code help???Any suggestions? any help??
the way i did it i used an arraylist called "currentDirectory" everything i added to this arraylist was of type "FileDetails", which was a class that defines a few things, name, absolutepath, size, type.
first i added a parent to the currentDirectory (not in the virtual root, but worry about this stuff later).
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">currentDirectory = new ArrayList();
currentDirectory.Add(new FileDetails("..",location,FileDetails.TYPE_PARENT);
[/QUOTE]
so when the user clicks on the filetdetails of type parent it moves up a dir.
add all the dirs
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">foreach(string dir in Directory.GetDirectories(loc))
currentDirectory.Add(new FileDetails(dir.substring(dir.lastindexOf("\\")+1),dir, FileDetails.TYPE_DIR)
[/QUOTE]
and the files.
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">foreach(string file in Directory.GetFiles(loc))
currentDirectory.Add(new FileDetails(dir.substring(dir.lastindexOf("\\")+1),file, FileDetails.TYPE_FILE)
[/QUOTE]
then just check what type the it is when the user clicks on it, if parent move up a dir, if sub, enter dir, if file ...
you might want to keep a integer of the STEP. so STEP=0 is the base dir, moving into a sub folder the step increases by one, moving to parent decreases by one. this way the user cant move beyond the specified original folder.
in the onkeypress method (i think its onkeypress, well onkey something method), just use a check like
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">switch(((FileDetails)currentDirectory[selectedFile]).Type){
case FileDetails.TYPE_PARENT:
//move up a dir
break;
case FileDetails.TYPE_DIR:
//move to dir
break;
case FileDetails.TYPE_FILE:
//open file
break;
}[/QUOTE]
thats basically what i did with the my videos/pics/music/programs plugins.
oh and a sample filedetails class
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">public class FileDetails
public const byte TYPE_PARENT = 0;
public const byte TYPE_FOLDER = 1;
public const byte TYPE_FILE = 2;
public string DisplayName{
get{
return displayName;
}
}
public string AbsolutePath{
get{
return absolutePath;
}
}
public byte Type{
get{
return type;
}
}
}
[/QUOTE]
if you want to use a graphic for each item you should put it in this method, also possible a dispose method to be called when you change dir eg.
in main class
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">foreach(FileDetails file in currentDirectory)
file.Dispose();
[/QUOTE]
and in FileDetails add a class like
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">public void Dispose(){
if(fileImage != null)
fileImage.Dispose();
fileImage = null;
}[/QUOTE]
where fileImage is just an image ie
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private Image fileImage = null;[/QUOTE]
calling the dispose method will just help with memory management.
You can also mix types in an arraylist. Â That is, maybe the arraylist itself would represent your directory, and if there are subdirectories, then they would be arraylists as well.
We won't have Generics until .NET 2.0, so if you iterate through a mixed collection, you'll need to work with objects and type checking/casting, which makes this approach a little more difficult.
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">foreach (object o in myArraylist)
{
 if (o is ArrayList)
 {
   ArrayList ao = o as ArrayList;
   // you can work with ao now
 }
 else if (o is SomeClass)
 {
   SomeClass sco = o as SomeClass;
   // you can work with sco now
 }
 else if (o is SomeValueType)
 {
   SomeValueType svto = (SomeValueType) o;
   // the "as" operator only works with classes
 }
}
[/QUOTE]