Quickest way to find out if a folder has subfolders???

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
What is the quickest way (by means of processor time) to find out if a folder has subfolders?
 
Easiest is to use the System.IO namespace
Visual Basic:
If System.IO.Directory.GetDirectories("c:\\test").Length > 0 Then
    'has sub folders
End If
as to if it performs the quickest then you could probably get an increase in speed by going to the windows API - just how much a performance difference remains to be seen though.
 
Last edited:
There is a specific win32 api function in the shell api dll for this.

Code:
public sealed class Shlwapi
{
	/*
	BOOL PathIsDirectoryEmpty(
	         LPCTSTR pszPath
		);
	*/
	[DllImport("shlwapi.dll",CharSet=CharSet.Auto,SetLastError=true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	public static extern bool PathIsDirectoryEmpty(
		[In,MarshalAs(UnmanagedType.LPTStr)] string pszPath
		);
}

The c# managed method will call the underlying api and construct an array of directory names, multiple string marshalling steps if the directory isn't empty increasing in overhead as the number of entries increases.
The FindFirstFile method requires strings and handles to be marshalled in and out and will require multiple calls because it returns . and .. entries, plus it needs the FindClose() call.
This single call marshals one string out and an int/bool back. It probably uses FindFirstFile internally but that is handled in native code so it incurs no marshalling overhead.

This is all conjecture obviously, if someone wants to work up test cases and test the speed i'd be interested to know if i'm right.
 
C# in VB

Wraith I'm really interested in your C# code because the Directory.GetDirectories.Lenght is really slow if the directory contains lots of subdirectories. And I only need to find out if the folder has subdirectories or not. I don't need to know how many there are. How can I use the C# code in my VB application?


I borrowed yeasterday "Sams Teach Yourself C# in 21 Days" and I'm only on day1 so I don't know anything about it.
 
Just translate it into VB, the function declaration doesn't use anything that doesn't have a direct analogue in VB so its a relatively straightforward task. Look up each part of the declaration, work out the VB syntax, next section.

Totally untested:
Code:
Public NotInheritable Class ShlWapi

    'BOOL PathIsDirectoryEmpty(
    '         LPCTSTR pszPath
    '	);

    <DllImport("shlwapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function PathIsDirectoryEmpty( _
             <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal pszPath As String _
        ) _
        As <MarshalAs(UnmanagedType.Bool)> Boolean
        ' leave body emtpy
    End Function

    ' or 

    Declare Auto Function PathIsDirectoryEmpty Lib "shlwapi.dll" _
    (<InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal pszPath As String) _
    As <MarshalAs(UnmanagedType.Bool)> Boolean

End Class

I'd suggest you learn c# because it'll help you a lot when you need to use code samples from c# in VB and there seem to be very few useful code samples in VB.
VB syntax is really annoying when it comes to pinvoke, the c# may look a bit messy but at least i don't have to mess around with stupid line continuations and bizzare pascal style assignment operators for attributes.
 
Thank you!

Wraith said:
Visual Basic:
    Declare Auto Function PathIsDirectoryEmpty Lib "shlwapi.dll" _
    (<InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal pszPath As String) _
    As <MarshalAs(UnmanagedType.Bool)> Boolean

Thanks Wraith! This one worked well and faster than GetDirectories! :)
 
Back
Top