Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

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.

Posted

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.

Posted

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! :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...