Wednesday, June 3, 2009

Wednesday, March 19, 2008

Find out the Special Directory path using .net with COM+

In .net APIs, you have one namespaces says, System.Environment.SpecialFolders, and with that, you can find the path of certain windows folder like, Program files, windows, my documents, and in .net 2.0, you have some more like My pictures, My music, etc.

But lets say for example if want to know the common user start menu, common user programs folder, common user application folder, then .net APIs can not help you.

Here is the solution to do that with the help of COM+ APIs, and you can find the path for all the special folders in windows XP and higher.

What you have to do is.

1. Create the enumuration for Namly UserType which can contains, CURRENT USER OR DEFAULT USER

i.e

public enum UserType
{
CURRENT = 0, // current value for user, verify it exists
DEFAULT = 1, // default value, may not exist
}

====================================================================


2. Create the class file that contains the extnernal method define in shell32.dll file.

i.e


public class ShellObj
{
[DllImport("Shell32.dll")]
public static extern int SHGetFolderPath(IntPtr owner, CSIDL nFolder,
IntPtr accessToken, SHGFP_TYPE dwFlags, StringBuilder path);

[DllImport("Shell32.dll")]
public static extern bool SHGetSpecialFolderPath(IntPtr owner, StringBuilder path,
CSIDL folder, bool create);
}


====================================================================

3. Now define one enumration that contains the list of all the folder address given in windows in hex format.


public enum CSIDL
{
CSIDL_FLAG_CREATE = 0x8000,
CSIDL_ADMINTOOLS = 0x0030,
CSIDL_ALTSTARTUP = 0x001d,
CSIDL_APPDATA = 0x001a,
CSIDL_BITBUCKET = 0x000a,
CSIDL_CDBURNAREA = 0x003b,
CSIDL_COMMON_ADMINTOOLS = 0x002f,
CSIDL_COMMON_ALTSTARTUP = 0x001e,
CSIDL_COMMON_APPDATA = 0x0023,
CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019,
CSIDL_COMMON_DOCUMENTS = 0x002e,
CSIDL_COMMON_FAVORITES = 0x001f,
CSIDL_COMMON_MUSIC = 0x0035,
CSIDL_COMMON_PICTURES = 0x0036,
CSIDL_COMMON_PROGRAMS = 0x0017,
CSIDL_COMMON_STARTMENU = 0x0016,
CSIDL_COMMON_STARTUP = 0x0018,
CSIDL_COMMON_TEMPLATES = 0x002d,
CSIDL_COMMON_VIDEO = 0x0037,
CSIDL_CONTROLS = 0x0003,
CSIDL_COOKIES = 0x0021,
CSIDL_DESKTOP = 0x0000,
CSIDL_DESKTOPDIRECTORY = 0x0010,
CSIDL_DRIVES = 0x0011,
CSIDL_FAVORITES = 0x0006,
CSIDL_FONTS = 0x0014,
CSIDL_HISTORY = 0x0022,
CSIDL_INTERNET = 0x0001,
CSIDL_INTERNET_CACHE = 0x0020,
CSIDL_LOCAL_APPDATA = 0x001c,
CSIDL_MYDOCUMENTS = 0x000c,
CSIDL_MYMUSIC = 0x000d,
CSIDL_MYPICTURES = 0x0027,
CSIDL_MYVIDEO = 0x000e,
CSIDL_NETHOOD = 0x0013,
CSIDL_NETWORK = 0x0012,
CSIDL_PERSONAL = 0x0005,
CSIDL_PRINTERS = 0x0004,
CSIDL_PRINTHOOD = 0x001b,
CSIDL_PROFILE = 0x0028,
CSIDL_PROFILES = 0x003e,
CSIDL_PROGRAM_FILES = 0x0026,
CSIDL_PROGRAM_FILES_COMMON = 0x002b,
CSIDL_PROGRAMS = 0x0002,
CSIDL_RECENT = 0x0008,
CSIDL_SENDTO = 0x0009,
CSIDL_STARTMENU = 0x000b,
CSIDL_STARTUP = 0x0007,
CSIDL_SYSTEM = 0x0025,
CSIDL_TEMPLATES = 0x0015,
CSIDL_WINDOWS = 0x0024
}


4. Thats all we need to do, now using these three definations you can find the path of all type of special directories that windows XP or higher has.

As an exmaple, you can write down the code like this as well,


public class SpecialFolder
{
public string CommonStartMenuDirectory
{
get
{
StringBuilder path = new StringBuilder(256);
ShellObj.SHGetFolderPath(IntPtr.Zero, CSIDL.CSIDL_COMMON_STARTMENU, IntPtr.Zero,
SHGFP_TYPE.CURRENT, path);
return path.ToString();
}
}
}


Enjoy the Coding .... :)
socl