JumpyNET Posted May 9, 2005 Posted May 9, 2005 What is the quickest way (by means of processor time) to find out if a folder has subfolders? Quote
Administrators PlausiblyDamp Posted May 9, 2005 Administrators Posted May 9, 2005 (edited) Easiest is to use the System.IO namespace 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. Edited May 9, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Wraith Posted May 9, 2005 Posted May 9, 2005 There is a specific win32 api function in the shell api dll for this. 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. Quote
JumpyNET Posted May 11, 2005 Author Posted May 11, 2005 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. Quote
Wraith Posted May 11, 2005 Posted May 11, 2005 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: 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. Quote
JumpyNET Posted May 12, 2005 Author Posted May 12, 2005 Thank you! 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! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.