Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

C# does Shell, Part 4

, 26 Mar 2003 Ms-PL
This article is about the AutoComplete features enabled by windows and how to use them with C#. AutoComplete is the ability to expand strings written in an edit box. The article will develop a class for using this functionality in you applications.
using System;
using System.Runtime.InteropServices;

namespace ShellLib
{
	[ComVisible(true)]
	[Guid("3766C955-DA6F-4fbc-AD36-311E342EF180")]
	public class FilterByExtension : IFolderFilter
	{
		// Allows a client to specify which individual items should be enumerated.
		// Note: The host calls this method for each item in the folder. Return S_OK, to have the item enumerated. 
		// Return S_FALSE to prevent the item from being enumerated.
		public Int32 ShouldShow(
			Object psf,				// A pointer to the folder's IShellFolder interface.
			IntPtr pidlFolder,		// The folder's PIDL.
			IntPtr pidlItem)		// The item's PIDL.
		{
			// check extension, and if not ok return 1 (S_FALSE)
			
			// get display name of item
			ShellLib.IShellFolder isf = (ShellLib.IShellFolder)psf;

			ShellLib.ShellApi.STRRET ptrDisplayName;
			isf.GetDisplayNameOf(pidlItem,(uint)ShellLib.ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellLib.ShellApi.SHGNO.SHGDN_FORPARSING,out ptrDisplayName);
			
			String sDisplay;
			ShellLib.ShellApi.StrRetToBSTR(ref ptrDisplayName,(IntPtr)0,out sDisplay);
			
			// check if item is file or folder
			IntPtr[] aPidl = new IntPtr[1];
			aPidl[0] = pidlItem;
			uint Attrib;
			Attrib = (uint)ShellLib.ShellApi.SFGAO.SFGAO_FOLDER;

			int temp;
			temp = isf.GetAttributesOf(1,aPidl,ref Attrib);

			// if item is a folder, accept
			if ((Attrib & (uint)ShellLib.ShellApi.SFGAO.SFGAO_FOLDER) == (uint)ShellLib.ShellApi.SFGAO.SFGAO_FOLDER)
				return 0;

			// if item is file, check if it has a valid extension
			for (int i=0 ; i<ValidExtension.Length ; i++)
			{
				if (sDisplay.ToUpper().EndsWith("." + ValidExtension[i].ToUpper()))
					return 0;
			}

			return 1;
		}

		// Allows a client to specify which classes of objects in a Shell folder should be enumerated.
		public Int32 GetEnumFlags( 
			Object psf,				// A pointer to the folder's IShellFolder interface.
			IntPtr pidlFolder,		// The folder's PIDL.
			IntPtr phwnd,			// A pointer to the host's window handle.
			out UInt32 pgrfFlags)	// One or more SHCONTF values that specify which classes of objects to enumerate.
		{
			pgrfFlags = (uint)ShellApi.SHCONTF.SHCONTF_FOLDERS | (uint)ShellApi.SHCONTF.SHCONTF_NONFOLDERS;
			return 0;
		}

		public string[] ValidExtension;
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.
 
Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 27 Mar 2003
Article Copyright 2003 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid