Game Development Reference
In-Depth Information
foreach (var directory in directoryInfo.GetDirectories())
{
FileAttributes attributes = File.GetAttributes(directory.FullName);
if ((attributes & FileAttributes.Hidden) == 0 )
{
var childDirectoryNode = new TreeNode(directory.Name);
childDirectoryNode.Tag = directory;
currentNode.Nodes.Add(childDirectoryNode);
stack.Push(childDirectoryNode);
}
}
foreach (var file in directoryInfo.GetFiles())
{
FileAttributes attributes = File.GetAttributes(file.FullName);
if ((attributes & FileAttributes.Hidden) == 0 )
{
var childNode = new TreeNode(file.Name);
childNode.Tag = file.FullName;
currentNode.Nodes.Add(childNode);
}
}
}
TreeView_Assets.Nodes.Add(node);
}
First, notice that the method uses an iterative algorithm rather than a recursive one,
which isn
t absolutely required when walking a directory tree but is typically a safer
way to initialize a tree structure. A recursive algorithm uses stack space to store data
and therefore opens the possibility of overflowing the stack. Iterative algorithms can
do the same work in less memory, although they are a little harder to read. Second,
if hidden files are found in the directory structure, they aren
'
'
t added to the tree.
Ignoring hidden files can be really useful if you use a source code repository like
SVN, which stores additional information in hidden .svn directories. Finally, for con-
venience, the TreeNode.Tag member is initialized to the full
file path of the
filename.
Notice the var keyword used to declare variables? That is C#
'
s equivalent of C++
'
s
auto keyword, which can be a real convenience without losing strong typing.
A game editor should always make it convenient to open an asset file, which can be
managed easily with the following code:
private void TreeView_Assets_MouseDoubleClick(object sender, MouseEventArgs e)
{
Search WWH ::




Custom Search