private void InitializeInstructionTree()
        {
            InstructionsTree.Items.Clear();
            TreeViewItemInstruction instructionTreeNode = CreateInstructionTreeNode(Settings.Default.InstructionsDirectory);

            InstructionsTree.Items.Add(instructionTreeNode);
            BuildInstructionTreeBranch(instructionTreeNode);
        }
        private void BuildInstructionTreeBranch(TreeViewItemInstruction selectedNode)
        {
            string[] directories = Directory.GetDirectories(selectedNode.FullPath);
            foreach (string directory in directories)
            {
                try
                {
                    Directory.GetDirectories(directory);
                    TreeViewItemInstruction newChild = null;
                    foreach (TreeViewItemInstruction currentChild in selectedNode.Items)
                    {
                        if (currentChild.FullPath == directory)
                        {
                            newChild = currentChild;
                            break;
                        }
                    }
                    if (newChild == null)
                    {
                        newChild = CreateInstructionTreeNode(directory);
                        selectedNode.Items.Add(newChild);
                        if (directories.Length > 0 && !IsInstructionDirectory(newChild.FullPath))
                        {
                            newChild.Expanded += ExpandtInstructionDirectory;
                        }
                    }
                    if (!IsInstructionDirectory(directory))
                    {
                        foreach (string subDirectory in Directory.GetDirectories(directory))
                        {
                            try
                            {
                                //Check for permision.
                                Directory.GetDirectories(subDirectory);

                                TreeViewItemInstruction instructionTreeNode = CreateInstructionTreeNode(subDirectory);
                                newChild.Items.Add(instructionTreeNode);
                                if (directories.Length > 0)
                                {
                                    newChild.Expanded += ExpandtInstructionDirectory;
                                }
                            }
                            catch (Exception) { }
                        }
                    }
                }
                catch (Exception) { }
            }
        }
        public void ExpandtInstructionDirectory(object sender, RoutedEventArgs e)
        {
            TreeViewItemInstruction selectedNode = (TreeViewItemInstruction)sender;

            if (!selectedNode.Items.IsEmpty)
            {
                foreach (TreeViewItemInstruction child in selectedNode.Items)
                {
                    if (!child.Items.IsEmpty)
                    {
                        return;
                    }
                }
            }
            BuildInstructionTreeBranch(selectedNode);
        }
        private TreeViewItemInstruction CreateInstructionTreeNode(string directoryPath)
        {
            TreeViewItemInstruction newNode = new TreeViewItemInstruction
            {
                FullPath = directoryPath
            };

            if (Path.GetFileName(directoryPath).Trim() == "")
            {
                newNode.Header = directoryPath;
            }
            else
            {
                newNode.Header = Path.GetFileName(directoryPath);
            }
            if (IsInstructionDirectory(newNode.FullPath))
            {
                newNode.Selected   += InstructionSelected;
                newNode.Unselected += InstructionUnselected;
                newNode.Background  = Brushes.Orange;
            }
            return(newNode);
        }