Skip to navigation
Real World Computing

Almost ADSL

Posted on 2 Jul 2002 at 17:04

Dave Jewell awaits his ADSL installation with both fear and excitement, and shows how to access the Shell32 COM objects from C#

This creates a new instance of the Shell object found inside the Shell32 library. This library contains a variety of objects, most of which are implemented for scripting purposes and for use by Visual Basic developers, but there's no reason why we can't make use of them from a C# application too. Armed with a new Shell object there are numerous methods you can play with. One of the most useful is BrowseForFolder, which brings up the standard browse-for-folder dialog. You can invoke the dialog as easily as this:

sh.BrowseForFolder (Handle, "Choose Folder", 0, null);

Handle is the API-level window handle of your C# form, which becomes the parent window of the new dialog, but you can pass zero for this parameter if you wish. The next argument is a prompt string that appears in the Browse For Folder dialog, while the third parameter allows you to specify one or more special options such as browsing for printers and browsing for computers on the LAN. You can find more details on these options in the Microsoft SDK - normally you'd just pass zero here. The final argument designates the root folder to use for the browse operation, and if you pass null you can browse all of Explorer's namespace, right up the Desktop level. If you want to, for example, limit browsing to C drive, then specify this as the root folder like so:

sh.BrowseForFolder (Handle, "Choose Folder", 0, "C:");

How do you know which folder the user selected? Well, the function result of the call is a new object of type System32.Folder:

Shell32.Folder f = sh.BrowseForFolder (Handle, 'Choose Folder", 0, null);

Of course, you can't just dive in and start doing things with the returned Folder object because the user might have selected Cancel in the Browse For Folder dialog. If they did, the returned object reference will be null and things will go pop if you try to use it, so always check the function result first. Once you've got a reference to a Folder object, you can figure out how many items it contains like this:

int NumFiles = folder.Items().Count;

If you've done much DOS or Windows programming, you need to watch out for a little wrinkle here: traditionally, Microsoft has counted the pseudo directory entries '.' (this directory) and '..' (the parent directory) as valid entries within a subdirectory, so that no subdirectory could ever contain zero items as it had to have at least these two. This has now changed and these pseudo-entries are no longer counted, which most of the time makes for neater code, but you need to guard against doing something like this:

Shell32.FolderItem item = folder.Items().Item (0);

If you were to execute this code without checking the file count and the subdirectory happened to contain no files, an exception would be raised. Why? Because the Items collection would be empty and this code would represent an attempt to retrieve its non-existent first item - always make sure that the zero-based index value is less than the returned file count. Here's a simple code snippet that responds to a button-click by displaying the browse-for-folder dialog and then populating a list box with the contents of the chosen folder.

protected void button1_Click (object sender, System.EventArgs e)

{

Shell32.Shell sh = new Shell32.Shell();

Shell32.Folder folder = sh.BrowseForFolder (Handle, "Choose Folder", 0, null);

if (folder != null) {

FList.Items.Clear();

int NumFiles = folder.Items().Count;

for (int Idx = 0; Idx < NumFiles; Idx++) {

Shell32.FolderItem item = folder.Items().Item (Idx);

1 2 3 4
Subscribe to PC Pro magazine. We'll give you 3 issues for £1 plus a free gift - click here

From around the web

Be the first to comment this article

You need to Login or Register to comment.

(optional)

advertisement

Latest Real World Computing
Latest Blog Posts Subscribe to our RSS Feeds
Latest News Stories Subscribe to our RSS Feeds
Latest ReviewsSubscribe to our RSS Feeds

advertisement

Sponsored Links
 
SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2010
 
 

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.