Here's an interesting one.
I was futzing around today with the various classes in the System.IO namespace that deal with the file system (e.g. File, Directory, Path, FileInfo, etc). Is it just me or does it seem like an omission to not have something like an .IsEmpty property? Granted, this functionality doesn't exist even in the Windows API as such, but still...the .NET framework is all about making the task of developing software easier, more accessible.
Sure, there are a few strategies that one could employ to determine if a folder is indeed empty (none of which I like). They usually involve getting the list of files and subdirectories and determining if there are none. I don't really like these approaches because they involve getting the full list of files and folders first just to compare the count to zero (0). For example:
private bool isDirectoryEmpty(string path) { string[] subDirs = Directory.GetDirectories(path); if ( 0 == subDirs.Length ) { string[] files = Directory.GetFiles(path); return ( 0 == files.Length ); } return false;}
Suppose the directory contained hundreds or thousands of files and/or directories? A bunch of cycles are wasted simply enumerating all of the files only to discard the results. Is there a better way using the .NET framework?
I propose there is and I have a better solution, but I'd like your thoughts and ideas. I'll post my answer in another post. (I'm hoping there's something built-in that I'm simply overlooking, but I'm not holding out for it)
(BTW: I really think you should be able to have code that resembles the following and be done with it. I guess I could propose it to the .NET team (which I'll do). If nothing else, we could wait until .NET 3.0 when we have extension methods at our disposal.)
if ( Directory.IsEmpty(@"C:\SomePath") ) { // happily proceed knowing that it was empty at the moment we called the method // ...but might not be at this moment :)}
Remember Me
a@href@title, b, i, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008R. Aaron Zupancic
E-mail