Advertisement
armega

Access Special Folders Not Listed In SHGetFolderPath API

Jan 16th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 33.50 KB | None | 0 0
  1. //For more information on this check out the following URL:
  2. // http://stackoverflow.com/questions/27910592/get-path-to-links-aka-favorites-folder
  3.  
  4. //WinAPI.cs
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Runtime.InteropServices;
  10.  
  11. namespace WinAPIs
  12. {
  13.     public class WinAPI
  14.     {
  15.         #region Shell32
  16.         #region Public class constants
  17.         public const int MAX_PATH = 256;
  18.         public const uint BIF_RETURNONLYFSDIRS = 0x0001;
  19.         public const uint BIF_DONTGOBELOWDOMAIN = 0x0002;
  20.         public const uint BIF_STATUSTEXT = 0x0004;
  21.         public const uint BIF_RETURNFSANCESTORS = 0x0008;
  22.         public const uint BIF_EDITBOX = 0x0010;
  23.         public const uint BIF_VALIDATE = 0x0020;
  24.         public const uint BIF_NEWDIALOGSTYLE = 0x0040;
  25.         public const uint BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX);
  26.         public const uint BIF_BROWSEINCLUDEURLS = 0x0080;
  27.         public const uint BIF_BROWSEFORCOMPUTER = 0x1000;
  28.         public const uint BIF_BROWSEFORPRINTER = 0x2000;
  29.         public const uint BIF_BROWSEINCLUDEFILES = 0x4000;
  30.         public const uint BIF_SHAREABLE = 0x8000;
  31.         public const uint SHGFI_ICON = 0x000000100;     // get icon
  32.         public const uint SHGFI_DISPLAYNAME = 0x000000200;     // get display name
  33.         public const uint SHGFI_TYPENAME = 0x000000400;     // get type name
  34.         public const uint SHGFI_ATTRIBUTES = 0x000000800;     // get attributes
  35.         public const uint SHGFI_ICONLOCATION = 0x000001000;     // get icon location
  36.         public const uint SHGFI_EXETYPE = 0x000002000;     // return exe type
  37.         public const uint SHGFI_SYSICONINDEX = 0x000004000;     // get system icon index
  38.         public const uint SHGFI_LINKOVERLAY = 0x000008000;     // put a link overlay on icon
  39.         public const uint SHGFI_SELECTED = 0x000010000;     // show icon in selected state
  40.         public const uint SHGFI_ATTR_SPECIFIED = 0x000020000;     // get only specified attributes
  41.         public const uint SHGFI_LARGEICON = 0x000000000;     // get large icon
  42.         public const uint SHGFI_SMALLICON = 0x000000001;     // get small icon
  43.         public const uint SHGFI_OPENICON = 0x000000002;     // get open icon
  44.         public const uint SHGFI_SHELLICONSIZE = 0x000000004;     // get shell size icon
  45.         public const uint SHGFI_PIDL = 0x000000008;     // pszPath is a pidl
  46.         public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;     // use passed dwFileAttribute
  47.         public const uint SHGFI_ADDOVERLAYS = 0x000000020;     // apply the appropriate overlays
  48.         public const uint SHGFI_OVERLAYINDEX = 0x000000040;     // Get the index of the overlay
  49.         public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
  50.         public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
  51.         #endregion
  52.  
  53.         #region Public class externs
  54.         [DllImport("Shell32.dll")]
  55.         public static extern IntPtr SHGetFileInfo(
  56.             string pszPath,
  57.             uint dwFileAttributes,
  58.             ref SHFILEINFO psfi,
  59.             uint cbFileInfo,
  60.             uint uFlags
  61.         );
  62.         [DllImport("shell32.dll")]
  63.         public static extern int SHGetKnownFolderPath(
  64.              [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
  65.              uint dwFlags,
  66.              IntPtr hToken,
  67.              out IntPtr pszPath
  68.         );
  69.         #endregion
  70.  
  71.         #region Public class structures
  72.         [StructLayout(LayoutKind.Sequential)]
  73.         public struct SHITEMID
  74.         {
  75.             public ushort cb;
  76.             [MarshalAs(UnmanagedType.LPArray)]
  77.             public byte[] abID;
  78.         }
  79.  
  80.         [StructLayout(LayoutKind.Sequential)]
  81.         public struct ITEMIDLIST
  82.         {
  83.             public SHITEMID mkid;
  84.         }
  85.  
  86.         [StructLayout(LayoutKind.Sequential)]
  87.         public struct BROWSEINFO
  88.         {
  89.             public IntPtr hwndOwner;
  90.             public IntPtr pidlRoot;
  91.             public IntPtr pszDisplayName;
  92.             [MarshalAs(UnmanagedType.LPTStr)]
  93.             public string lpszTitle;
  94.             public uint ulFlags;
  95.             public IntPtr lpfn;
  96.             public int lParam;
  97.             public IntPtr iImage;
  98.         }
  99.  
  100.         [StructLayout(LayoutKind.Sequential)]
  101.         public struct SHFILEINFO
  102.         {
  103.             public const int NAMESIZE = 80;
  104.             public IntPtr hIcon;
  105.             public int iIcon;
  106.             public uint dwAttributes;
  107.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
  108.             public string szDisplayName;
  109.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
  110.             public string szTypeName;
  111.         }
  112.         #endregion
  113.  
  114.         #region Public classes
  115.         public static class KnownFolder
  116.         {
  117.             public static readonly Guid AddNewPrograms = new Guid("de61d971-5ebc-4f02-a3a9-6c82895e5c04");
  118.             public static readonly Guid AdminTools = new Guid("724EF170-A42D-4FEF-9F26-B60E846FBA4F");
  119.             public static readonly Guid AppUpdates = new Guid("a305ce99-f527-492b-8b1a-7e76fa98d6e4");
  120.             public static readonly Guid CDBurning = new Guid("9E52AB10-F80D-49DF-ACB8-4330F5687855");
  121.             public static readonly Guid ChangeRemovePrograms = new Guid("df7266ac-9274-4867-8d55-3bd661de872d");
  122.             public static readonly Guid CommonAdminTools = new Guid("D0384E7D-BAC3-4797-8F14-CBA229B392B5");
  123.             public static readonly Guid CommonOEMLinks = new Guid("C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D");
  124.             public static readonly Guid CommonPrograms = new Guid("0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8");
  125.             public static readonly Guid CommonStartMenu = new Guid("A4115719-D62E-491D-AA7C-E74B8BE3B067");
  126.             public static readonly Guid CommonStartup = new Guid("82A5EA35-D9CD-47C5-9629-E15D2F714E6E");
  127.             public static readonly Guid CommonTemplates = new Guid("B94237E7-57AC-4347-9151-B08C6C32D1F7");
  128.             public static readonly Guid ComputerFolder = new Guid("0AC0837C-BBF8-452A-850D-79D08E667CA7");
  129.             public static readonly Guid ConflictFolder = new Guid("4bfefb45-347d-4006-a5be-ac0cb0567192");
  130.             public static readonly Guid ConnectionsFolder = new Guid("6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD");
  131.             public static readonly Guid Contacts = new Guid("56784854-C6CB-462b-8169-88E350ACB882");
  132.             public static readonly Guid ControlPanelFolder = new Guid("82A74AEB-AEB4-465C-A014-D097EE346D63");
  133.             public static readonly Guid Cookies = new Guid("2B0F765D-C0E9-4171-908E-08A611B84FF6");
  134.             public static readonly Guid Desktop = new Guid("B4BFCC3A-DB2C-424C-B029-7FE99A87C641");
  135.             public static readonly Guid Documents = new Guid("FDD39AD0-238F-46AF-ADB4-6C85480369C7");
  136.             public static readonly Guid Downloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");
  137.             public static readonly Guid Favorites = new Guid("1777F761-68AD-4D8A-87BD-30B759FA33DD");
  138.             public static readonly Guid Fonts = new Guid("FD228CB7-AE11-4AE3-864C-16F3910AB8FE");
  139.             public static readonly Guid Games = new Guid("CAC52C1A-B53D-4edc-92D7-6B2E8AC19434");
  140.             public static readonly Guid GameTasks = new Guid("054FAE61-4DD8-4787-80B6-090220C4B700");
  141.             public static readonly Guid History = new Guid("D9DC8A3B-B784-432E-A781-5A1130A75963");
  142.             public static readonly Guid InternetCache = new Guid("352481E8-33BE-4251-BA85-6007CAEDCF9D");
  143.             public static readonly Guid InternetFolder = new Guid("4D9F7874-4E0C-4904-967B-40B0D20C3E4B");
  144.             public static readonly Guid Links = new Guid("bfb9d5e0-c6a9-404c-b2b2-ae6db6af4968");
  145.             public static readonly Guid LocalAppData = new Guid("F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
  146.             public static readonly Guid LocalAppDataLow = new Guid("A520A1A4-1780-4FF6-BD18-167343C5AF16");
  147.             public static readonly Guid LocalizedResourcesDir = new Guid("2A00375E-224C-49DE-B8D1-440DF7EF3DDC");
  148.             public static readonly Guid Music = new Guid("4BD8D571-6D19-48D3-BE97-422220080E43");
  149.             public static readonly Guid NetHood = new Guid("C5ABBF53-E17F-4121-8900-86626FC2C973");
  150.             public static readonly Guid NetworkFolder = new Guid("D20BEEC4-5CA8-4905-AE3B-BF251EA09B53");
  151.             public static readonly Guid OriginalImages = new Guid("2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39");
  152.             public static readonly Guid PhotoAlbums = new Guid("69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C");
  153.             public static readonly Guid Pictures = new Guid("33E28130-4E1E-4676-835A-98395C3BC3BB");
  154.             public static readonly Guid Playlists = new Guid("DE92C1C7-837F-4F69-A3BB-86E631204A23");
  155.             public static readonly Guid PrintersFolder = new Guid("76FC4E2D-D6AD-4519-A663-37BD56068185");
  156.             public static readonly Guid PrintHood = new Guid("9274BD8D-CFD1-41C3-B35E-B13F55A758F4");
  157.             public static readonly Guid Profile = new Guid("5E6C858F-0E22-4760-9AFE-EA3317B67173");
  158.             public static readonly Guid ProgramData = new Guid("62AB5D82-FDC1-4DC3-A9DD-070D1D495D97");
  159.             public static readonly Guid ProgramFiles = new Guid("905e63b6-c1bf-494e-b29c-65b732d3d21a");
  160.             public static readonly Guid ProgramFilesX64 = new Guid("6D809377-6AF0-444b-8957-A3773F02200E");
  161.             public static readonly Guid ProgramFilesX86 = new Guid("7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E");
  162.             public static readonly Guid ProgramFilesCommon = new Guid("F7F1ED05-9F6D-47A2-AAAE-29D317C6F066");
  163.             public static readonly Guid ProgramFilesCommonX64 = new Guid("6365D5A7-0F0D-45E5-87F6-0DA56B6A4F7D");
  164.             public static readonly Guid ProgramFilesCommonX86 = new Guid("DE974D24-D9C6-4D3E-BF91-F4455120B917");
  165.             public static readonly Guid Programs = new Guid("A77F5D77-2E2B-44C3-A6A2-ABA601054A51");
  166.             public static readonly Guid Public = new Guid("DFDF76A2-C82A-4D63-906A-5644AC457385");
  167.             public static readonly Guid PublicDesktop = new Guid("C4AA340D-F20F-4863-AFEF-F87EF2E6BA25");
  168.             public static readonly Guid PublicDocuments = new Guid("ED4824AF-DCE4-45A8-81E2-FC7965083634");
  169.             public static readonly Guid PublicDownloads = new Guid("3D644C9B-1FB8-4f30-9B45-F670235F79C0");
  170.             public static readonly Guid PublicGameTasks = new Guid("DEBF2536-E1A8-4c59-B6A2-414586476AEA");
  171.             public static readonly Guid PublicMusic = new Guid("3214FAB5-9757-4298-BB61-92A9DEAA44FF");
  172.             public static readonly Guid PublicPictures = new Guid("B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5");
  173.             public static readonly Guid PublicVideos = new Guid("2400183A-6185-49FB-A2D8-4A392A602BA3");
  174.             public static readonly Guid QuickLaunch = new Guid("52a4f021-7b75-48a9-9f6b-4b87a210bc8f");
  175.             public static readonly Guid Recent = new Guid("AE50C081-EBD2-438A-8655-8A092E34987A");
  176.             public static readonly Guid RecycleBinFolder = new Guid("B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC");
  177.             public static readonly Guid ResourceDir = new Guid("8AD10C31-2ADB-4296-A8F7-E4701232C972");
  178.             public static readonly Guid RoamingAppData = new Guid("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D");
  179.             public static readonly Guid SampleMusic = new Guid("B250C668-F57D-4EE1-A63C-290EE7D1AA1F");
  180.             public static readonly Guid SamplePictures = new Guid("C4900540-2379-4C75-844B-64E6FAF8716B");
  181.             public static readonly Guid SamplePlaylists = new Guid("15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5");
  182.             public static readonly Guid SampleVideos = new Guid("859EAD94-2E85-48AD-A71A-0969CB56A6CD");
  183.             public static readonly Guid SavedGames = new Guid("4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4");
  184.             public static readonly Guid SavedSearches = new Guid("7d1d3a04-debb-4115-95cf-2f29da2920da");
  185.             public static readonly Guid SEARCH_CSC = new Guid("ee32e446-31ca-4aba-814f-a5ebd2fd6d5e");
  186.             public static readonly Guid SEARCH_MAPI = new Guid("98ec0e18-2098-4d44-8644-66979315a281");
  187.             public static readonly Guid SearchHome = new Guid("190337d1-b8ca-4121-a639-6d472d16972a");
  188.             public static readonly Guid SendTo = new Guid("8983036C-27C0-404B-8F08-102D10DCFD74");
  189.             public static readonly Guid SidebarDefaultParts = new Guid("7B396E54-9EC5-4300-BE0A-2482EBAE1A26");
  190.             public static readonly Guid SidebarParts = new Guid("A75D362E-50FC-4fb7-AC2C-A8BEAA314493");
  191.             public static readonly Guid StartMenu = new Guid("625B53C3-AB48-4EC1-BA1F-A1EF4146FC19");
  192.             public static readonly Guid Startup = new Guid("B97D20BB-F46A-4C97-BA10-5E3608430854");
  193.             public static readonly Guid SyncManagerFolder = new Guid("43668BF8-C14E-49B2-97C9-747784D784B7");
  194.             public static readonly Guid SyncResultsFolder = new Guid("289a9a43-be44-4057-a41b-587a76d7e7f9");
  195.             public static readonly Guid SyncSetupFolder = new Guid("0F214138-B1D3-4a90-BBA9-27CBC0C5389A");
  196.             public static readonly Guid System = new Guid("1AC14E77-02E7-4E5D-B744-2EB1AE5198B7");
  197.             public static readonly Guid SystemX86 = new Guid("D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27");
  198.             public static readonly Guid Templates = new Guid("A63293E8-664E-48DB-A079-DF759E0509F7");
  199.             public static readonly Guid TreeProperties = new Guid("5b3749ad-b49f-49c1-83eb-15370fbd4882");
  200.             public static readonly Guid UserProfiles = new Guid("0762D272-C50A-4BB0-A382-697DCD729B80");
  201.             public static readonly Guid UsersFiles = new Guid("f3ce0f7c-4901-4acc-8648-d5d44b04ef8f");
  202.             public static readonly Guid Videos = new Guid("18989B1D-99B5-455B-841C-AB7C74E4DDFC");
  203.             public static readonly Guid Windows = new Guid("F38BF404-1D43-42F2-9305-67DE0B28FC23");
  204.         }
  205.         #endregion
  206.         #endregion
  207.  
  208.         #region User32
  209.         #region Public class externs
  210.         [DllImport("User32.dll")]
  211.         public static extern int DestroyIcon(IntPtr hIcon);
  212.         #endregion
  213.         #endregion
  214.     }
  215. }
  216.  
  217. // SpecialFolders.cs
  218. using System;
  219. using System.Collections.Generic;
  220. using System.Linq;
  221. using System.Text;
  222. using System.Drawing;
  223. using System.Diagnostics;
  224. using WinAPIs;
  225.  
  226. namespace FileFolderWorker
  227. {
  228.     #region Classes used as return values
  229.     public class FolderProperties
  230.     {
  231.         #region Private class variables
  232.         private string m_sPath = "";
  233.         private Icon m_oIcon = null;
  234.         #endregion
  235.  
  236.         #region Public class properties
  237.         public string Path
  238.         {
  239.             get
  240.             {
  241.                 return m_sPath;
  242.             }
  243.             set
  244.             {
  245.                 m_sPath = value;
  246.             }
  247.         }
  248.  
  249.         public Icon Icon
  250.         {
  251.             get
  252.             {
  253.                 return m_oIcon;
  254.             }
  255.             set
  256.             {
  257.                 m_oIcon = value;
  258.             }
  259.         }
  260.         #endregion
  261.     }
  262.     #endregion
  263.  
  264.     public static class SpecialFolders
  265.     {
  266.         #region Enums
  267.         public enum FolderList
  268.         {
  269.             None,
  270.             AdminTools,
  271.             ApplicationData,
  272.             CDBurning,
  273.             CommonAdminTools,
  274.             CommonApplicationData,
  275.             CommonDesktopDirectory,
  276.             CommonDocuments,
  277.             CommonMusic,
  278.             CommonOemLinks,
  279.             CommonPictures,
  280.             CommonProgramFiles,
  281.             CommonProgramFilesX86,
  282.             CommonPrograms,
  283.             CommonStartMenu,
  284.             CommonStartup,
  285.             CommonTemplates,
  286.             CommonVideos,
  287.             Cookies,
  288.             Desktop,
  289.             DesktopDirectory,
  290.             Favorites,
  291.             Fonts,
  292.             History,
  293.             InternetCache,
  294.             LocalApplicationData,
  295.             LocalizedResources,
  296.             MyComputer,
  297.             MyDocuments,
  298.             MyMusic,
  299.             MyPictures,
  300.             MyVideos,
  301.             NetworkShortcuts,
  302.             Personal,
  303.             PrinterShortcuts,
  304.             ProgramFiles,
  305.             ProgramFilesX86,
  306.             Programs,
  307.             Recent,
  308.             Resources,
  309.             SendTo,
  310.             StartMenu,
  311.             Startup,
  312.             System,
  313.             SystemX86,
  314.             Templates,
  315.             UserProfile,
  316.             Windows,
  317.             Links,
  318.         }
  319.  
  320.         public enum FolderType
  321.         {
  322.             None,
  323.             System,
  324.             Custom,
  325.         }
  326.         #endregion
  327.  
  328.         #region Public static class methods
  329.         public static FolderProperties GetPath(string sFolderKey)
  330.         {
  331.             //Overloaded
  332.             FolderList FolderKey = FolderList.None;
  333.             //Determine the folder type
  334.             switch (sFolderKey)
  335.             {
  336.                 #region System's Environment.SpecialFolder elements
  337.                 case "AdminTools":
  338.                     FolderKey = FolderList.AdminTools;
  339.                     break;
  340.                 case "ApplicationData":
  341.                     FolderKey = FolderList.ApplicationData;
  342.                     break;
  343.                 case "CDBurning":
  344.                     FolderKey = FolderList.CDBurning;
  345.                     break;
  346.                 case "CommonAdminTools":
  347.                     FolderKey = FolderList.CommonAdminTools;
  348.                     break;
  349.                 case "CommonApplicationData":
  350.                     FolderKey = FolderList.CommonApplicationData;
  351.                     break;
  352.                 case "CommonDesktopDirectory":
  353.                     FolderKey = FolderList.CommonDesktopDirectory;
  354.                     break;
  355.                 case "CommonDocuments":
  356.                     FolderKey = FolderList.CommonDocuments;
  357.                     break;
  358.                 case "CommonMusic":
  359.                     FolderKey = FolderList.CommonMusic;
  360.                     break;
  361.                 case "CommonOemLinks":
  362.                     FolderKey = FolderList.CommonOemLinks;
  363.                     break;
  364.                 case "CommonPictures":
  365.                     FolderKey = FolderList.CommonPictures;
  366.                     break;
  367.                 case "CommonProgramFiles":
  368.                     FolderKey = FolderList.CommonProgramFiles;
  369.                     break;
  370.                 case "CommonProgramFilesX86":
  371.                     FolderKey = FolderList.CommonProgramFilesX86;
  372.                     break;
  373.                 case "CommonPrograms":
  374.                     FolderKey = FolderList.CommonPrograms;
  375.                     break;
  376.                 case "CommonStartMenu":
  377.                     FolderKey = FolderList.CommonStartMenu;
  378.                     break;
  379.                 case "CommonStartup":
  380.                     FolderKey = FolderList.CommonStartup;
  381.                     break;
  382.                 case "CommonTemplates":
  383.                     FolderKey = FolderList.CommonTemplates;
  384.                     break;
  385.                 case "CommonVideos":
  386.                     FolderKey = FolderList.CommonVideos;
  387.                     break;
  388.                 case "Cookies":
  389.                     FolderKey = FolderList.Cookies;
  390.                     break;
  391.                 case "Desktop":
  392.                     FolderKey = FolderList.Desktop;
  393.                     break;
  394.                 case "DesktopDirectory":
  395.                     FolderKey = FolderList.DesktopDirectory;
  396.                     break;
  397.                 case "Favorites":
  398.                     FolderKey = FolderList.Favorites;
  399.                     break;
  400.                 case "Fonts":
  401.                     FolderKey = FolderList.Fonts;
  402.                     break;
  403.                 case "History":
  404.                     FolderKey = FolderList.History;
  405.                     break;
  406.                 case "InternetCache":
  407.                     FolderKey = FolderList.InternetCache;
  408.                     break;
  409.                 case "LocalApplicationData":
  410.                     FolderKey = FolderList.LocalApplicationData;
  411.                     break;
  412.                 case "LocalizedResources":
  413.                     FolderKey = FolderList.LocalizedResources;
  414.                     break;
  415.                 case "MyComputer":
  416.                     FolderKey = FolderList.MyComputer;
  417.                     break;
  418.                 case "MyDocuments":
  419.                     FolderKey = FolderList.MyDocuments;
  420.                     break;
  421.                 case "MyMusic":
  422.                     FolderKey = FolderList.MyMusic;
  423.                     break;
  424.                 case "MyPictures":
  425.                     FolderKey = FolderList.MyPictures;
  426.                     break;
  427.                 case "MyVideos":
  428.                     FolderKey = FolderList.MyVideos;
  429.                     break;
  430.                 case "NetworkShortcuts":
  431.                     FolderKey = FolderList.NetworkShortcuts;
  432.                     break;
  433.                 case "Personal":
  434.                     FolderKey = FolderList.Personal;
  435.                     break;
  436.                 case "PrinterShortcuts":
  437.                     FolderKey = FolderList.PrinterShortcuts;
  438.                     break;
  439.                 case "ProgramFiles":
  440.                     FolderKey = FolderList.ProgramFiles;
  441.                     break;
  442.                 case "ProgramFilesX86":
  443.                     FolderKey = FolderList.ProgramFilesX86;
  444.                     break;
  445.                 case "Programs":
  446.                     FolderKey = FolderList.Programs;
  447.                     break;
  448.                 case "Recent":
  449.                     FolderKey = FolderList.Recent;
  450.                     break;
  451.                 case "Resources":
  452.                     FolderKey = FolderList.Resources;
  453.                     break;
  454.                 case "SendTo":
  455.                     FolderKey = FolderList.SendTo;
  456.                     break;
  457.                 case "StartMenu":
  458.                     FolderKey = FolderList.StartMenu;
  459.                     break;
  460.                 case "Startup":
  461.                     FolderKey = FolderList.Startup;
  462.                     break;
  463.                 case "System":
  464.                     FolderKey = FolderList.System;
  465.                     break;
  466.                 case "SystemX86":
  467.                     FolderKey = FolderList.SystemX86;
  468.                     break;
  469.                 case "Templates":
  470.                     FolderKey = FolderList.Templates;
  471.                     break;
  472.                 case "UserProfile":
  473.                     FolderKey = FolderList.UserProfile;
  474.                     break;
  475.                 case "Windows":
  476.                     FolderKey = FolderList.Windows;
  477.                     break;
  478.                 #endregion
  479.  
  480.                 #region Custom elements
  481.                 case "Links":
  482.                     FolderKey = FolderList.Links;
  483.                     break;
  484.                 #endregion
  485.             }
  486.  
  487.             return GetPath(FolderKey);
  488.         }
  489.         public static FolderProperties GetPath(FolderList FolderKey)
  490.         {
  491.             FolderProperties fp = new FolderProperties();
  492.             FolderType sfType = FolderType.None;
  493.             Environment.SpecialFolder sf = Environment.SpecialFolder.AdminTools;
  494.  
  495.             //Determine the folder type
  496.             switch (FolderKey)
  497.             {
  498.                 #region System's Environment.SpecialFolder elements
  499.                 case FolderList.AdminTools:
  500.                     sfType = FolderType.System;
  501.                     sf = Environment.SpecialFolder.AdminTools;
  502.                     break;
  503.                 case FolderList.ApplicationData:
  504.                     sfType = FolderType.System;
  505.                     sf = Environment.SpecialFolder.ApplicationData;
  506.                     break;
  507.                 case FolderList.CDBurning:
  508.                     sfType = FolderType.System;
  509.                     sf = Environment.SpecialFolder.CDBurning;
  510.                     break;
  511.                 case FolderList.CommonAdminTools:
  512.                     sfType = FolderType.System;
  513.                     sf = Environment.SpecialFolder.CommonAdminTools;
  514.                     break;
  515.                 case FolderList.CommonApplicationData:
  516.                     sfType = FolderType.System;
  517.                     sf = Environment.SpecialFolder.CommonApplicationData;
  518.                     break;
  519.                 case FolderList.CommonDesktopDirectory:
  520.                     sfType = FolderType.System;
  521.                     sf = Environment.SpecialFolder.CommonDesktopDirectory;
  522.                     break;
  523.                 case FolderList.CommonDocuments:
  524.                     sfType = FolderType.System;
  525.                     sf = Environment.SpecialFolder.CommonDocuments;
  526.                     break;
  527.                 case FolderList.CommonMusic:
  528.                     sfType = FolderType.System;
  529.                     sf = Environment.SpecialFolder.CommonMusic;
  530.                     break;
  531.                 case FolderList.CommonOemLinks:
  532.                     sfType = FolderType.System;
  533.                     sf = Environment.SpecialFolder.CommonOemLinks;
  534.                     break;
  535.                 case FolderList.CommonPictures:
  536.                     sfType = FolderType.System;
  537.                     sf = Environment.SpecialFolder.CommonPictures;
  538.                     break;
  539.                 case FolderList.CommonProgramFiles:
  540.                     sfType = FolderType.System;
  541.                     sf = Environment.SpecialFolder.CommonProgramFiles;
  542.                     break;
  543.                 case FolderList.CommonProgramFilesX86:
  544.                     sfType = FolderType.System;
  545.                     sf = Environment.SpecialFolder.CommonProgramFilesX86;
  546.                     break;
  547.                 case FolderList.CommonPrograms:
  548.                     sfType = FolderType.System;
  549.                     sf = Environment.SpecialFolder.CommonPrograms;
  550.                     break;
  551.                 case FolderList.CommonStartMenu:
  552.                     sfType = FolderType.System;
  553.                     sf = Environment.SpecialFolder.CommonStartMenu;
  554.                     break;
  555.                 case FolderList.CommonStartup:
  556.                     sfType = FolderType.System;
  557.                     sf = Environment.SpecialFolder.CommonStartup;
  558.                     break;
  559.                 case FolderList.CommonTemplates:
  560.                     sfType = FolderType.System;
  561.                     sf = Environment.SpecialFolder.CommonTemplates;
  562.                     break;
  563.                 case FolderList.CommonVideos:
  564.                     sfType = FolderType.System;
  565.                     sf = Environment.SpecialFolder.CommonVideos;
  566.                     break;
  567.                 case FolderList.Cookies:
  568.                     sfType = FolderType.System;
  569.                     sf = Environment.SpecialFolder.Cookies;
  570.                     break;
  571.                 case FolderList.Desktop:
  572.                     sfType = FolderType.System;
  573.                     sf = Environment.SpecialFolder.Desktop;
  574.                     break;
  575.                 case FolderList.DesktopDirectory:
  576.                     sfType = FolderType.System;
  577.                     sf = Environment.SpecialFolder.DesktopDirectory;
  578.                     break;
  579.                 case FolderList.Favorites:
  580.                     sfType = FolderType.System;
  581.                     sf = Environment.SpecialFolder.Favorites;
  582.                     break;
  583.                 case FolderList.Fonts:
  584.                     sfType = FolderType.System;
  585.                     sf = Environment.SpecialFolder.Fonts;
  586.                     break;
  587.                 case FolderList.History:
  588.                     sfType = FolderType.System;
  589.                     sf = Environment.SpecialFolder.History;
  590.                     break;
  591.                 case FolderList.InternetCache:
  592.                     sfType = FolderType.System;
  593.                     sf = Environment.SpecialFolder.InternetCache;
  594.                     break;
  595.                 case FolderList.LocalApplicationData:
  596.                     sfType = FolderType.System;
  597.                     sf = Environment.SpecialFolder.LocalApplicationData;
  598.                     break;
  599.                 case FolderList.LocalizedResources:
  600.                     sfType = FolderType.System;
  601.                     sf = Environment.SpecialFolder.LocalizedResources;
  602.                     break;
  603.                 case FolderList.MyComputer:
  604.                     sfType = FolderType.System;
  605.                     sf = Environment.SpecialFolder.MyComputer;
  606.                     break;
  607.                 case FolderList.MyDocuments:
  608.                     sfType = FolderType.System;
  609.                     sf = Environment.SpecialFolder.MyDocuments;
  610.                     break;
  611.                 case FolderList.MyMusic:
  612.                     sfType = FolderType.System;
  613.                     sf = Environment.SpecialFolder.MyMusic;
  614.                     break;
  615.                 case FolderList.MyPictures:
  616.                     sfType = FolderType.System;
  617.                     sf = Environment.SpecialFolder.MyPictures;
  618.                     break;
  619.                 case FolderList.MyVideos:
  620.                     sfType = FolderType.System;
  621.                     sf = Environment.SpecialFolder.MyVideos;
  622.                     break;
  623.                 case FolderList.NetworkShortcuts:
  624.                     sfType = FolderType.System;
  625.                     sf = Environment.SpecialFolder.NetworkShortcuts;
  626.                     break;
  627.                 case FolderList.Personal:
  628.                     sfType = FolderType.System;
  629.                     sf = Environment.SpecialFolder.Personal;
  630.                     break;
  631.                 case FolderList.PrinterShortcuts:
  632.                     sfType = FolderType.System;
  633.                     sf = Environment.SpecialFolder.PrinterShortcuts;
  634.                     break;
  635.                 case FolderList.ProgramFiles:
  636.                     sfType = FolderType.System;
  637.                     sf = Environment.SpecialFolder.ProgramFiles;
  638.                     break;
  639.                 case FolderList.ProgramFilesX86:
  640.                     sfType = FolderType.System;
  641.                     sf = Environment.SpecialFolder.ProgramFilesX86;
  642.                     break;
  643.                 case FolderList.Programs:
  644.                     sfType = FolderType.System;
  645.                     sf = Environment.SpecialFolder.Programs;
  646.                     break;
  647.                 case FolderList.Recent:
  648.                     sfType = FolderType.System;
  649.                     sf = Environment.SpecialFolder.Recent;
  650.                     break;
  651.                 case FolderList.Resources:
  652.                     sfType = FolderType.System;
  653.                     sf = Environment.SpecialFolder.Resources;
  654.                     break;
  655.                 case FolderList.SendTo:
  656.                     sfType = FolderType.System;
  657.                     sf = Environment.SpecialFolder.SendTo;
  658.                     break;
  659.                 case FolderList.StartMenu:
  660.                     sfType = FolderType.System;
  661.                     sf = Environment.SpecialFolder.StartMenu;
  662.                     break;
  663.                 case FolderList.Startup:
  664.                     sfType = FolderType.System;
  665.                     sf = Environment.SpecialFolder.Startup;
  666.                     break;
  667.                 case FolderList.System:
  668.                     sfType = FolderType.System;
  669.                     sf = Environment.SpecialFolder.System;
  670.                     break;
  671.                 case FolderList.SystemX86:
  672.                     sfType = FolderType.System;
  673.                     sf = Environment.SpecialFolder.SystemX86;
  674.                     break;
  675.                 case FolderList.Templates:
  676.                     sfType = FolderType.System;
  677.                     sf = Environment.SpecialFolder.Templates;
  678.                     break;
  679.                 case FolderList.UserProfile:
  680.                     sfType = FolderType.System;
  681.                     sf = Environment.SpecialFolder.UserProfile;
  682.                     break;
  683.                 case FolderList.Windows:
  684.                     sfType = FolderType.System;
  685.                     sf = Environment.SpecialFolder.Windows;
  686.                     break;
  687.                 #endregion
  688.  
  689.                 #region Custom elements
  690.                 case FolderList.Links:
  691.                     sfType = FolderType.Custom;
  692.                     break;
  693.                 #endregion
  694.             }
  695.  
  696.             //Build the folder object's path
  697.             switch (sfType)
  698.             {
  699.                 case FolderType.System:
  700.                     fp.Path = Environment.GetFolderPath(sf);
  701.                     break;
  702.                 case FolderType.Custom:
  703.                     fp.Path = "LINKS";
  704.                     fp.Path = GetSpecialFolderPath(WinAPI.KnownFolder.Links);
  705.                     break;
  706.             }
  707.  
  708.             //Build the folder object's icon
  709.             //more to be done here...
  710.  
  711.             return fp;
  712.         }
  713.  
  714.         public static void DebugShowAllFolders()
  715.         {
  716.             foreach (SpecialFolders.FolderList sf in (SpecialFolders.FolderList[])Enum.GetValues(typeof(SpecialFolders.FolderList)))
  717.             {
  718.                 Debug.WriteLine("SpecialFolder: " + sf + "\n  Path: " + SpecialFolders.GetPath(sf.ToString()).Path + "\n");
  719.             }
  720.         }
  721.  
  722.         public static string GetSpecialFolderPath(Guid kFolderID)
  723.         {
  724.             string sRet = "";
  725.  
  726.             IntPtr pPath;
  727.             if (WinAPI.SHGetKnownFolderPath(kFolderID, 0, IntPtr.Zero, out pPath) == 0)
  728.             {
  729.                 sRet = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
  730.                 System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
  731.             }
  732.  
  733.             return sRet;
  734.         }
  735.         #endregion
  736.     }
  737. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement