Fast way to retrieve sub folder names without the full path
<p>I know there are duplicates out there, but I have tried about 10 methods and it takes between 6ms and 29 ms, with an average of about 10 ms for each folder to parse out the name of the file from the path returned in <code>Directory.GetDirectories()</code>.</p>

<p>The fastest I have found is <code>System.IO.Path.GetFileName(fullPath)</code>, but only by a short margin of <code>string.SubString(string.LastIndexOf(@"\")</code>;</p>

<p>I am writing a better version of Windows Explorer, but it takes about half a second to expand the <code>C:\</code> and show all sub folders. Half a second may not seem like much, but Windows, and another program I looked at do it what appears instantly.</p>

<p>The only thing I can think of is to index the file and store the index as XML, I guess that can be done at startup or something.</p>

<p>I am just curious how Windows and another control can do it so much faster on the same PC. Is C# and managed code slower than unmanaged C++?</p>

<p>Update:</p>

<p>Here is a sample of the log entry, I realize using File.AppendAllText has to open and close the file, but many of the operations only take 1 milisecond to accomplish this, so the only slow time is the SetDirectoryName, which is done like:</p>

<pre><code>public void LoadChildNodes()
        {
            // clear the child nodes
            this.ChildPanel.Controls.Clear();

            // if this path exists
            if (Directory.Exists(this.Path))
            {
                // get the log
                WriteLogEntry("After Path Exists: " + stopWatch.Elapsed.Milliseconds.ToString());

                // get the directories for this node
                string[] tempDirectories = Directory.GetDirectories(this.Path);

                // get the log
                WriteLogEntry("After GetDirectories: " + stopWatch.Elapsed.Milliseconds.ToString());

                // if there rae one or more directories
                if ((tempDirectories != null) &amp;&amp; (tempDirectories.Length &gt; 0))
                {  
                    // reverse the list
                    List&lt;string&gt; directories = new List&lt;string&gt;();

                    // iterate the strings
                    foreach (string tempDirectory in tempDirectories)
                    {
                        // add this item
                        directories.Add(tempDirectory);
                    }

                    // now set the directories
                    directories.Reverse();

                    // log the time
                    WriteLogEntry("After Reverse Directories: " + stopWatch.Elapsed.Milliseconds.ToString());

                    // iterate the directory names
                    foreach (string directory in directories)
                    {
                        // log the time
                        WriteLogEntry("After Start Iterate New Directory: " + stopWatch.Elapsed.Milliseconds.ToString());

                        // create the childNode
                        ExplorerTreeNode childNode = new ExplorerTreeNode();

                        // the path for folders is the same as the name
                        string directoryName = System.IO.Path.GetFileName(directory);

                        // log the time
                        WriteLogEntry("After set directory name: " + stopWatch.Elapsed.Milliseconds.ToString());

                        // setup the node
                        childNode.SetupNode(directoryName, NodeTypeEnum.Folder, this.IconManager, this.Font, path);

                        // log the time
                        WriteLogEntry("After Setup Node" + stopWatch.Elapsed.Milliseconds.ToString());

                        // add this node
                        this.ChildPanel.Controls.Add(childNode);

                        // log the time
                        WriteLogEntry("After Add childNode to Controls: " + stopWatch.Elapsed.Milliseconds.ToString());

                        // dock to top
                        childNode.Dock = DockStyle.Top;

                        // log the time
                        WriteLogEntry("After Dock: " + stopWatch.Elapsed.Milliseconds.ToString());
                    }

                    // finished loading child nodes
                    stopWatch.Stop();
                    WriteLogEntry("Finished loading child nodes: " + stopWatch.Elapsed.Milliseconds.ToString());
                }                 
            }
        }
</code></pre>

<p>I was trying to avoid buying a control so I could make the project open source, but I guess I will just buy it and only give the executable away.</p>

<p>After Path Exists: 1
After GetDirectories: 2
After Reverse Directories: 3
After Start Iterate New Directory: 3
After set directory name: 20
After Setup Node21
After Add childNode to Controls: 21
After Dock: 22
After Start Iterate New Directory: 22
After set directory name: 29
After Setup Node29
After Add childNode to Controls: 30
After Dock: 30
After Start Iterate New Directory: 30
After set directory name: 37
After Setup Node38
After Add childNode to Controls: 38
After Dock: 39
After Start Iterate New Directory: 39</p>
