private void SortNodes(TreeNodeSimulation tns)
        {
            if (tns.Nodes.Count == 0)
            {
                return;
            }

            tns.Nodes.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true));

            foreach (var t in tns.Nodes)
            {
                SortNodes(t);
            }
        }
        private void BindTree(TreeNodeSimulation visual, TreeNodeCollection nodes)
        {
            var n = new TreeNode(visual.Name);
            n.Name = visual.Name;
            n.Tag = visual.Tag;
            n.SelectedImageIndex = 7;
            n.ImageIndex = visual.ImageIndex;

            nodes.Add(n);

            if (visual.Nodes.Count == 0)
                return;

            foreach (var child in visual.Nodes)
            {
                BindTree(child, n.Nodes);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = Settings.Default["InitialDirectory"].ToString();

            openFileDialog1.Filter = "Solution File (*.sln)|*.sln";
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                Task t = Task.Factory.StartNew(() =>
                {
                    Invoke((MethodInvoker)delegate
                    {
                        toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
                        toolStripProgressBar1.MarqueeAnimationSpeed = 30;
                        toolStripLabel1.Text = "Loading...";

                        if (treeView1.Nodes.Count > 0)
                        {
                            for (int i = treeView1.Nodes.Count - 1; i >= 0; i--)
                            {
                                treeView1.Nodes[i].Remove();
                            }
                        }

                        _fileList = new List<string>();
                    });

                    _basedPath = Path.GetDirectoryName(openFileDialog1.FileName);

                    Settings.Default["InitialDirectory"] = _basedPath;
                    Settings.Default.Save();

                    _solutionNode = new TreeNodeSimulation(Path.GetFileName(openFileDialog1.FileName), openFileDialog1.FileName, 0);

                    var projectCount = 0;
                    try
                    {
                        Regex regex = new Regex("Project.* = ([^,]*),([^,]*)");
                        System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);

                        string line;
                        while ((line = file.ReadLine()) != null)
                        {
                            var match = regex.Match(line);
                            if (match.Success)
                            {
                                var path = Path.Combine(_basedPath, match.Value
                                                                    .Substring(match.Value.LastIndexOf(',') + 1)
                                                                    .Replace("\"", "")
                                                                    .Trim());
                                projectCount++;
                                var projectBasedPath = Path.GetDirectoryName(path);

                                var projNode = new TreeNodeSimulation(Path.GetFileName(path), path, 1);
                                _solutionNode.Nodes.Add(projNode);
                                var doc = new XmlDocument();
                                doc.Load(path);

                                var nsmgr = new XmlNamespaceManager(doc.NameTable);
                                nsmgr.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003");
                                var includedFiles = doc.SelectNodes(@"//ms:Compile[@Include] | //ms:Content[@Include]", nsmgr);

                                foreach (var f in includedFiles)
                                {
                                    var fnode = f as XmlNode;
                                    var n = fnode.Attributes.GetNamedItem("Include");
                                    var temp = n.Value;

                                    var segments = temp.Split(new[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (segments[segments.Length - 1].Length <= 12 ||
                                        (segments[segments.Length - 1].Substring(segments[segments.Length - 1].Length - 12, 12).ToLower() != ".designer.vb" &&
                                        Path.GetExtension(segments[segments.Length - 1]).ToLower() != ".resx" &&
                                        Path.GetExtension(segments[segments.Length - 1]).ToLower() != ".dll" &&
                                        segments[segments.Length - 1].ToLower() != "assemblyinfo.vb"))
                                    {
                                        if (segments.Length == 1)
                                        {

                                            var fNode = new TreeNodeSimulation(segments[0], Path.Combine(projectBasedPath, temp), GetImageIndex(segments[0]));
                                            _fileList.Add(fNode.Tag + string.Empty);
                                            projNode.Nodes.Add(fNode);
                                        }
                                        else
                                        {
                                            var parN = projNode;

                                            for (var i = 0; i < segments.Length - 1; i++)
                                            {
                                                var curN = parN.Nodes.FirstOrDefault(p => p.Name == segments[i]);

                                                if (curN == null)
                                                {

                                                    var currPath = string.Empty;
                                                    for (var j = 0; j <= i; j++)
                                                    {
                                                        currPath += segments[j] + "\\";
                                                    }

                                                    curN = new TreeNodeSimulation(segments[i], Path.Combine(projectBasedPath, currPath), 2);
                                                    parN.Nodes.Add(curN);
                                                }

                                                parN = curN;
                                            }

                                            var fileName = segments[segments.Length - 1];
                                            var leafN = new TreeNodeSimulation(fileName, Path.Combine(projectBasedPath, temp), GetImageIndex(fileName));
                                            _fileList.Add(leafN.Tag + string.Empty);
                                            parN.Nodes.Add(leafN);
                                        }
                                    }
                                }
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Invoke((MethodInvoker)delegate
                        {
                            toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
                            toolStripProgressBar1.MarqueeAnimationSpeed = 0;
                            toolStripLabel1.Text = "Error: Could not read file from disk.";
                        });
                    }

                    SortNodes(_solutionNode);

                    Invoke((MethodInvoker)delegate
                    {
                        BindTree(_solutionNode, treeView1.Nodes);

                        toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
                        toolStripProgressBar1.MarqueeAnimationSpeed = 0;
                        toolStripLabel1.Text = "Loaded successfully " + _fileList.Count + " file(s) in " + projectCount + " project(s).";
                    });
                });

            }
        }