Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.ComponentModel
- Imports System.IO
- Imports System.Runtime.InteropServices
- ''' <summary>
- ''' Windows Tree View Control 1.0.0
- ''' Created by Eprouvez
- ''' Credits To Perry (http://glassocean.net/how-to-create-a-treeview-file-browser-component-in-vb-net/)
- ''' </summary>
- Public Class WindowsTreeView
- Inherits TreeView
- Private MyComputerIcon As String = KnownFolders.GetPath(KnownFolders.KnownFolder.SystemX86) & "\shell32.dll,15"
- Private RootNode As TreeNode
- Private _ImageList As New ImageList
- Public Sub New()
- 'SetStyle(ControlStyles.DoubleBuffer, True)
- ImageList = _ImageList
- Font = New Font("Segoe UI", 8.25)
- 'FullRowSelect = True
- Nodes.Clear()
- If Not LicenseManager.UsageMode = LicenseUsageMode.Designtime Then
- Try
- CacheIndexedIcon(New EmbeddedIconInfo(MyComputerIcon), "This PC")
- Catch ex As Exception
- CacheBlankIcon("This PC")
- End Try
- CacheBlankIcon("blank")
- RootNode = Me.Nodes.Add("This PC", "This PC", "This PC", "This PC")
- RefreshFolders()
- End If
- End Sub
- #Region " Routines & Functions "
- ''' <summary>
- ''' Refreshes all files and folders
- ''' </summary>
- Sub RefreshFolders()
- RootNode.Nodes.Clear()
- Me.BeginUpdate()
- For Each SpecialFolder As String In ThisPCDefault()
- Dim Node As New TreeNode(SpecialFolder.Substring(SpecialFolder.LastIndexOf("\") + 1))
- Node.Tag = SpecialFolder
- Node.ImageKey = CacheIcon(SpecialFolder, SpecialFolder)
- Node.SelectedImageKey = Node.ImageKey
- Node.Nodes.Add("*DUMMY*")
- RootNode.Nodes.Add(Node)
- Next
- For Each Drive In DriveInfo.GetDrives
- If Drive.DriveType = DriveType.Fixed Or DriveType.Removable Or DriveType.CDRom Then
- Dim Node As New TreeNode
- If Drive.IsReady Then
- If Drive.Name = Path.GetPathRoot(Environment.SystemDirectory) AndAlso Drive.VolumeLabel = "" Then
- Node.Text = "Local Disk (" & Drive.Name.Replace("\", ")")
- Else
- Node.Text = Drive.VolumeLabel & " (" & Drive.Name.Replace("\", ")")
- End If
- Node.Nodes.Add("*DUMMY*")
- ElseIf Drive.DriveType = DriveType.CDRom Then
- Node.Text = "CD Drive (" & Drive.Name.Replace("\", ")")
- Else
- Node.Text = Drive.Name
- Node.Nodes.Add("*DUMMY*")
- End If
- Node.Tag = Drive.RootDirectory
- Node.ImageKey = CacheIcon(Drive.RootDirectory.Name, Drive.Name)
- Node.SelectedImageKey = Node.ImageKey
- RootNode.Nodes.Add(Node)
- End If
- Next
- Me.EndUpdate()
- RootNode.Expand()
- End Sub
- Private Function ThisPCDefault() As String()
- Dim Paths As New List(Of String)
- With Paths
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Desktop))
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Documents))
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Downloads))
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Music))
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Pictures))
- .Add(KnownFolders.GetPath(KnownFolders.KnownFolder.Videos))
- End With
- Return Paths.ToArray
- End Function
- #End Region
- #Region " Overrides "
- Protected Overrides Sub WndProc(ByRef m As Message)
- If m.Msg = &H14 AndAlso Not LicenseManager.UsageMode = LicenseUsageMode.Designtime Then
- m.Msg = &H0
- End If
- MyBase.WndProc(m)
- End Sub
- Protected Overrides Sub OnBeforeExpand(e As TreeViewCancelEventArgs)
- MyBase.BeginUpdate()
- If Not e.Node.Text = "This PC" Then
- e.Node.Nodes.Clear()
- Dim Info As New DirectoryInfo(e.Node.Tag.ToString)
- Dim Directories As DirectoryInfo() = Info.GetDirectories
- Dim Files As FileInfo() = Info.GetFiles
- For Each Directory As DirectoryInfo In Directories
- Dim Node As New TreeNode(Directory.Name)
- Dim Extension As String = GetExtension(Directory.FullName)
- Node.Tag = Directory.FullName
- Node.ImageKey = Extension
- Node.SelectedImageKey = Node.ImageKey
- Node.Nodes.Add("*DUMMY*")
- e.Node.Nodes.Add(Node)
- Next
- CacheIcons(Directories)
- If _ShowFiles Then
- For Each File As FileInfo In Files
- Dim Node As New TreeNode(File.Name)
- Dim Extension As String = GetExtension(File.FullName)
- Node.Tag = File.FullName
- Node.ImageKey = Extension
- Node.SelectedImageKey = Node.ImageKey
- e.Node.Nodes.Add(Node)
- Next
- CacheIcons(Files)
- End If
- End If
- MyBase.EndUpdate()
- MyBase.OnBeforeExpand(e)
- End Sub
- Protected Overrides Sub OnBeforeCollapse(e As TreeViewCancelEventArgs)
- Me.BeginUpdate()
- If Not e.Node.Text = "This PC" Then
- e.Node.Nodes.Clear()
- e.Node.Nodes.Add("*DUMMY*")
- End If
- Me.EndUpdate()
- MyBase.OnBeforeCollapse(e)
- End Sub
- #End Region
- #Region " Properties "
- Private _ShowFiles As Boolean = False
- Public Property ShowFiles() As Boolean
- Get
- Return _ShowFiles
- End Get
- Set(ByVal value As Boolean)
- _ShowFiles = value
- RefreshFolders()
- End Set
- End Property
- #End Region
- #Region " Extract Indexed Icon "
- Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (Handle As Integer, FileName As String, Index As Integer) As IntPtr
- Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal FileName As String, ByVal Index As Integer, ByVal LargeSet() As IntPtr, ByVal SmallSet() As IntPtr, ByVal nIcons As Integer) As Integer
- Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Integer) As Integer
- Function ExtractIconFromFile(ByVal IconInfo As EmbeddedIconInfo) As Icon
- Return ExtractIconFromFile(IconInfo.FileName, IconInfo.IconIndex)
- End Function
- Function ExtractIconFromFile(ByVal File As String, Optional Index As Integer = 0) As Icon
- If Not IO.File.Exists(File) Then Return Nothing
- Dim Handle As IntPtr() = New IntPtr(0) {IntPtr.Zero}
- Dim Dummy As IntPtr() = New IntPtr(0) {IntPtr.Zero}
- Try
- Dim Count = ExtractIconEx(File, Index, Dummy, Handle, 1)
- Dim Icon As Icon = Icon.FromHandle(Handle(0)).Clone
- Return Icon
- Catch ex As Exception
- Return Nothing
- Finally
- For Each I As IntPtr In Handle
- If (Not I.Equals(IntPtr.Zero)) Then
- DestroyIcon(I)
- End If
- Next I
- For Each I As IntPtr In Dummy
- If Not (I.Equals(IntPtr.Zero)) Then
- DestroyIcon(I)
- End If
- Next I
- End Try
- End Function
- Function GetIndexedIconAsImage(ByVal Path As String, Optional Index As Integer = 0) As Image
- Dim Icon As Icon, Image As Image
- Dim Info As New EmbeddedIconInfo(Path, Index)
- Try
- Icon = ExtractIconFromFile(Info)
- Image = Icon.ToBitmap
- Catch ex As Exception
- Image = Nothing
- End Try
- Return Image
- End Function
- #End Region
- #Region " Extract Icon "
- Private _Handles As New List(Of IntPtr)
- Function GetIconAsImage(ByVal Path As String) As Image
- Dim Icon As Icon, Image As Image
- Dim FileInfo As New IconInfo
- FileInfo.Name = New String(Chr(0), 260)
- FileInfo.Type = New String(Chr(0), 80)
- SHGetFileInfo(Path, 0, FileInfo, Marshal.SizeOf(FileInfo), IconInfo.Icon Or IconInfo.SmallIcon)
- Try
- Icon = Icon.FromHandle(FileInfo.IconHandle).Clone
- Image = Icon.ToBitmap
- Catch ex As Exception
- Image = Nothing
- Finally
- DestroyIcon(FileInfo.IconHandle)
- '_Handles.Add(FileInfo.IconHandle)
- End Try
- Return Image
- End Function
- Function GetOpenIconAsImage(ByVal Path As String) As Image
- Dim Icon As Icon, Image As Image
- Dim FileInfo As New IconInfo
- FileInfo.Name = New String(Chr(0), 260)
- FileInfo.Type = New String(Chr(0), 80)
- SHGetFileInfo(Path, 0, FileInfo, Marshal.SizeOf(FileInfo), IconInfo.Icon Or IconInfo.SmallIcon Or IconInfo.OpenIcon)
- Try
- Icon = Icon.FromHandle(FileInfo.IconHandle).Clone
- Image = Icon.ToBitmap
- Catch ex As Exception
- Image = Nothing
- Finally
- _Handles.Add(FileInfo.IconHandle)
- End Try
- Return Image
- End Function
- #End Region
- #Region " Icon Information "
- Public Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal Path As String, ByVal Attributes As Integer, ByRef Info As IconInfo, ByVal InfoSize As Integer, ByVal Flags As Integer) As IntPtr
- Structure IconInfo
- Public Const Icon As Integer = &H100
- Public Const SmallIcon As Integer = &H1
- Public Const LargeIcon As Integer = &H0
- Public Const OpenIcon As Integer = &H2
- Public IconHandle As IntPtr
- Public Index As Integer
- Public Attributes As Integer
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
- Public Name As String
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)>
- Public Type As String
- End Structure
- Public Structure EmbeddedIconInfo
- Sub New(FileParam As String)
- If Not Parse(FileParam) Then Throw New Exception("No index given!")
- Dim Params As String() = FileParam.Split(",")
- _FileName = Params(0)
- _Index = Params(1)
- End Sub
- Sub New(FileName As String, Index As Integer)
- _FileName = FileName
- _Index = Index
- End Sub
- Shared Function Parse(File As String) As Boolean
- Return File.Contains(",")
- End Function
- Private _FileName As String
- Public Property FileName() As String
- Get
- Return _FileName
- End Get
- Set(ByVal value As String)
- _FileName = value
- End Set
- End Property
- Private _Index As Integer
- Public Property IconIndex() As Integer
- Get
- Return _Index
- End Get
- Set(ByVal value As Integer)
- _Index = value
- End Set
- End Property
- End Structure
- Shared Function GetEmbeddedIconInfo(Args As String) As EmbeddedIconInfo
- Dim Info As New EmbeddedIconInfo()
- If [String].IsNullOrEmpty(Args) Then Return Info
- Dim FileName As String = [String].Empty
- Dim Index As Integer = 0
- Dim StringIndex As String = [String].Empty
- Dim Seperate As Integer = Args.IndexOf(",")
- If Seperate > 0 Then
- FileName = Args.Substring(0, Seperate)
- StringIndex = Integer.Parse(Args.Substring(Seperate + 1))
- Else
- FileName = Args
- End If
- If Not [String].IsNullOrEmpty(StringIndex) Then
- Index = Integer.Parse(StringIndex)
- If Index < 0 Then Index = 0
- End If
- Info.FileName = FileName
- Info.IconIndex = Index
- Return Info
- End Function
- #End Region
- #Region " Icon Cache "
- Function CacheBlankIcon(Extension As String) As String
- If Extension = String.Empty Then Return Extension
- If _ImageList.Images.ContainsKey(Extension) Then Return Extension
- _ImageList.Images.Add(Extension, New Bitmap(16, 16))
- Return Extension
- End Function
- Function CacheIndexedIcon(ByVal Info As EmbeddedIconInfo, Extension As String) As String
- If _ImageList.Images.ContainsKey(Extension) Then Return Extension
- Try
- Dim ImageIcon As Image = ExtractIconFromFile(Info).ToBitmap
- _ImageList.Images.Add(Extension, ImageIcon)
- Catch ex As Exception
- CacheBlankIcon(Extension)
- End Try
- Return Extension
- End Function
- Function CacheIcon(ByVal Info As EmbeddedIconInfo, Optional Extension As String = "") As String
- Return CacheIcon(Info.FileName, Extension)
- End Function
- Private Delegate Function IconCacheDelegate(Path As String, Extension As String) As String
- Function CacheIcon(ByVal Path As String, Optional Extension As String = "") As String
- If InvokeRequired Then Invoke(New IconCacheDelegate(AddressOf CacheIcon), {Path, Extension})
- Dim Ext As String = Nothing
- Try
- If Extension = "" Then
- Ext = GetExtension(Path)
- Else
- Ext = Extension
- If Ext = ".exe" Or Ext = ".url" Or Ext = ".lnk" Then Ext = Path
- End If
- If Not _ImageList.Images.ContainsKey(Ext) Then
- Dim ImageIcon As Image = GetIconAsImage(Path)
- If ImageIcon IsNot Nothing Then
- _ImageList.Images.Add(Ext, ImageIcon)
- Else
- Return CacheBlankIcon(Extension)
- End If
- End If
- Catch ex As Exception
- Return CacheBlankIcon(Extension)
- End Try
- Return Ext
- End Function
- Sub CacheIcons(Paths As DirectoryInfo())
- Dim PathList As New List(Of String)
- For Each Path In Paths
- PathList.Add(Path.FullName)
- Next
- CacheIcons(PathList.ToArray)
- End Sub
- Sub CacheIcons(Paths As FileInfo())
- Dim PathList As New List(Of String)
- For Each Path In Paths
- PathList.Add(Path.FullName)
- Next
- CacheIcons(PathList.ToArray)
- End Sub
- Sub CacheIcons(Paths As String())
- Dim T As New Threading.Thread(AddressOf CacheAsync)
- T.IsBackground = True
- T.Start(Paths)
- End Sub
- Private Sub CacheAsync(Paths As String())
- For Each Path As String In Paths
- CacheIcon(Path)
- Application.DoEvents()
- Next
- End Sub
- Private Function GetExtension(Path As String) As String
- Dim Ext As String = ""
- If Directory.Exists(Path) Then
- Ext = "folder"
- ElseIf File.Exists(Path) Then
- Ext = IO.Path.GetExtension(Path)
- End If
- If Ext = ".exe" Or Ext = ".url" Or Ext = ".lnk" Then Ext = Path
- Return Ext
- End Function
- Private Sub WindowsTreeView_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
- End Sub
- #End Region
- Class WindowsFolderFile
- Sub New(Path As String)
- If Path = String.Empty Then Throw New Exception("")
- _Path = Path
- End Sub
- Function GetDirectories() As DirectoryInfo()
- Return New DirectoryInfo(_Path).GetDirectories
- End Function
- Function GetFiles() As FileInfo()
- Return New DirectoryInfo(_Path).GetFiles
- End Function
- Private _Path As String
- ReadOnly Property Path As String
- Get
- Return _Path
- End Get
- End Property
- Private _ImageKey As String
- ReadOnly Property ImageKey() As String
- Get
- Return _ImageKey
- End Get
- End Property
- Private _IsFolder As Boolean
- ReadOnly Property IsFolder() As Boolean
- Get
- Return _IsFolder
- End Get
- End Property
- Private _Extension As String
- ReadOnly Property Extension() As String
- Get
- Return _Extension
- End Get
- End Property
- End Class
- End Class
- Public NotInheritable Class KnownFolders
- #Region " Folder GUIDs "
- Private Shared FolderGUIDs As String() =
- {"{008CA0B1-55B4-4C56-B8A8-4DE4B299D3BE}",
- "{724EF170-A42D-4FEF-9F26-B60E846FBA4F}",
- "{A3918781-E5F2-4890-B3D9-A7E54332328C}",
- "{AB5FB87B-7CE2-4F83-915D-550846C9537B}",
- "{9E52AB10-F80D-49DF-ACB8-4330F5687855}",
- "{D0384E7D-BAC3-4797-8F14-CBA229B392B5}",
- "{C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}",
- "{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}",
- "{A4115719-D62E-491D-AA7C-E74B8BE3B067}",
- "{82A5EA35-D9CD-47C5-9629-E15D2F714E6E}",
- "{B94237E7-57AC-4347-9151-B08C6C32D1F7}",
- "{56784854-C6CB-462B-8169-88E350ACB882}",
- "{2B0F765D-C0E9-4171-908E-08A611B84FF6}",
- "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}",
- "{5CE4A5E9-E4EB-479D-B89F-130C02886155}",
- "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}",
- "{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}",
- "{374DE290-123F-4565-9164-39C4925E467B}",
- "{1777F761-68AD-4D8A-87BD-30B759FA33DD}",
- "{FD228CB7-AE11-4AE3-864C-16F3910AB8FE}",
- "{054FAE61-4DD8-4787-80B6-090220C4B700}",
- "{D9DC8A3B-B784-432E-A781-5A1130A75963}",
- "{BCB5256F-79F6-4CEE-B725-DC34E402FD46}",
- "{352481E8-33BE-4251-BA85-6007CAEDCF9D}",
- "{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}",
- "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}",
- "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}",
- "{A520A1A4-1780-4FF6-BD18-167343C5AF16}",
- "{2A00375E-224C-49DE-B8D1-440DF7EF3DDC}",
- "{4BD8D571-6D19-48D3-BE97-422220080E43}",
- "{2112AB0A-C86A-4FFE-A368-0DE96E47012E}",
- "{C5ABBF53-E17F-4121-8900-86626FC2C973}",
- "{2C36C0AA-5812-4B87-BFD0-4CD0DFB19B39}",
- "{69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}",
- "{A990AE9F-A03B-4E80-94BC-9912D7504104}",
- "{33E28130-4E1E-4676-835A-98395C3BC3BB}",
- "{DE92C1C7-837F-4F69-A3BB-86E631204A23}",
- "{9274BD8D-CFD1-41C3-B35E-B13F55A758F4}",
- "{5E6C858F-0E22-4760-9AFE-EA3317B67173}",
- "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}",
- "{905E63B6-C1BF-494E-B29C-65B732D3D21A}",
- "{6D809377-6AF0-444B-8957-A3773F02200E}",
- "{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}",
- "{F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}",
- "{6365D5A7-0F0D-45E5-87F6-0DA56B6A4F7D}",
- "{DE974D24-D9C6-4D3E-BF91-F4455120B917}",
- "{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}",
- "{DFDF76A2-C82A-4D63-906A-5644AC457385}",
- "{C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}",
- "{ED4824AF-DCE4-45A8-81E2-FC7965083634}",
- "{3D644C9B-1FB8-4F30-9B45-F670235F79C0}",
- "{DEBF2536-E1A8-4C59-B6A2-414586476AEA}",
- "{48DAF80B-E6CF-4F4E-B800-0E69D84EE384}",
- "{3214FAB5-9757-4298-BB61-92A9DEAA44FF}",
- "{B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}",
- "{E555AB60-153B-4D17-9F04-A5FE99FC15EC}",
- "{0482AF6C-08F1-4C34-8C90-E17EC98B1E17}",
- "{2400183A-6185-49FB-A2D8-4A392A602BA3}",
- "{52A4F021-7B75-48A9-9F6B-4B87A210BC8F}",
- "{AE50C081-EBD2-438A-8655-8A092E34987A}",
- "{1A6FDBA2-F42D-4358-A798-B74D745926C5}",
- "{8AD10C31-2ADB-4296-A8F7-E4701232C972}",
- "{C870044B-F49E-4126-A9C3-B52A1FF411E8}",
- "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}",
- "{AAA8D5A5-F1D6-4259-BAA8-78E7EF60835E}",
- "{00BCFC5A-ED94-4E48-96A1-3F6217F21990}",
- "{B250C668-F57D-4EE1-A63C-290EE7D1AA1F}",
- "{C4900540-2379-4C75-844B-64E6FAF8716B}",
- "{15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}",
- "{859EAD94-2E85-48AD-A71A-0969CB56A6CD}",
- "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}",
- "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}",
- "{B7BEDE81-DF94-4682-A7D8-57A52620B86F}",
- "{0D4C3DB6-03A3-462F-A0E6-08924C41B5D4}",
- "{7E636BFE-DFA9-4D5E-B456-D7B39851D8A9}",
- "{8983036C-27C0-404B-8F08-102D10DCFD74}",
- "{7B396E54-9EC5-4300-BE0A-2482EBAE1A26}",
- "{A75D362E-50FC-4FB7-AC2C-A8BEAA314493}",
- "{A52BBA46-E9E1-435F-B3D9-28DAA648C0F6}",
- "{767E6811-49CB-4273-87C2-20F355E1085B}",
- "{24D89E24-2F19-4534-9DDE-6A6671FBB8FE}",
- "{339719B5-8C47-4894-94C2-D8F77ADD44A6}",
- "{625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}",
- "{B97D20BB-F46A-4C97-BA10-5E3608430854}",
- "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}",
- "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}",
- "{A63293E8-664E-48DB-A079-DF759E0509F7}",
- "{9E3995AB-1F9C-4F13-B827-48B24B6C7174}",
- "{0762D272-C50A-4BB0-A382-697DCD729B80}",
- "{5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}",
- "{BCBD3057-CA5C-4622-B42D-BC56DB0AE516}",
- "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}",
- "{491E922F-5643-4AF4-A7EB-4E7A138D8174}",
- "{F38BF404-1D43-42F2-9305-67DE0B28FC23}"}
- #End Region
- #Region " Imports & Enums "
- ''' <summary>
- ''' Retrieves the full path of a known folder identified by the folder's KnownFolderID.
- ''' </summary>
- ''' <param name="rfid">A KnownFolderID that identifies the folder.</param>
- ''' <param name="dwFlags">Flags that specify special retrieval options. This value can be
- ''' 0; otherwise, one or more of the KnownFolderFlag values.</param>
- ''' <param name="hToken">An access token that represents a particular user. If this
- ''' parameter is NULL, which is the most common usage, the function requests the known
- ''' folder for the current user. Assigning a value of -1 indicates the Default User.
- ''' The default user profile is duplicated when any new user account is created.
- ''' Note that access to the Default User folders requires administrator privileges.
- ''' </param>
- ''' <param name="ppszPath">When this method returns, contains the address of a string that
- ''' specifies the path of the known folder. The returned path does not include a
- ''' trailing backslash.</param>
- ''' <returns>Returns S_OK if successful, or an error value otherwise.</returns>
- <DllImport("Shell32.dll")>
- Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> rfid As Guid, dwFlags As UInteger, hToken As IntPtr, ByRef ppszPath As IntPtr) As Integer
- End Function
- ''' <summary>
- ''' Standard folders registered with the system. These folders are installed with Windows Vista
- ''' and later operating systems, and a computer will have only folders appropriate to it
- ''' installed.
- ''' </summary>
- Public Enum KnownFolder
- AccountPictures
- AdminTools
- ApplicationShortcuts
- CameraRoll
- CDBurning
- CommonAdminTools
- CommonOemLinks
- CommonPrograms
- CommonStartMenu
- CommonStartup
- CommonTemplates
- Contacts
- Cookies
- Desktop
- DeviceMetadataStore
- Documents
- DocumentsLibrary
- Downloads
- Favorites
- Fonts
- GameTasks
- History
- ImplicitAppShortcuts
- InternetCache
- Libraries
- Links
- LocalAppData
- LocalAppDataLow
- LocalizedResourcesDir
- Music
- MusicLibrary
- NetHood
- OriginalImages
- PhotoAlbums
- PicturesLibrary
- Pictures
- Playlists
- PrintHood
- Profile
- ProgramData
- ProgramFiles
- ProgramFilesX64
- ProgramFilesX86
- ProgramFilesCommon
- ProgramFilesCommonX64
- ProgramFilesCommonX86
- Programs
- [Public]
- PublicDesktop
- PublicDocuments
- PublicDownloads
- PublicGameTasks
- PublicLibraries
- PublicMusic
- PublicPictures
- PublicRingtones
- PublicUserTiles
- PublicVideos
- QuickLaunch
- Recent
- RecordedTVLibrary
- ResourceDir
- Ringtones
- RoamingAppData
- RoamedTileImages
- RoamingTiles
- SampleMusic
- SamplePictures
- SamplePlaylists
- SampleVideos
- SavedGames
- SavedSearches
- Screenshots
- SearchHistory
- SearchTemplates
- SendTo
- SidebarDefaultParts
- SidebarParts
- SkyDrive
- SkyDriveCameraRoll
- SkyDriveDocuments
- SkyDrivePictures
- StartMenu
- Startup
- System
- SystemX86
- Templates
- UserPinned
- UserProfiles
- UserProgramFiles
- UserProgramFilesCommon
- Videos
- VideosLibrary
- Windows
- End Enum
- <Flags>
- Private Enum KnownFolderFlags As UInteger
- SimpleIDList = &H100
- NotParentRelative = &H200
- DefaultPath = &H400
- Init = &H800
- NoAlias = &H1000
- DontUnexpand = &H2000
- DontVerify = &H4000
- Create = &H8000
- NoAppcontainerRedirection = &H10000
- AliasOnly = &H80000000UI
- End Enum
- #End Region
- #Region " Sub Routines "
- ''' <summary>
- ''' Gets the current path to the specified known folder as currently configured. This does
- ''' not require the folder to be existent.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which current path will be returned.</param>
- ''' <returns>The default path of the known folder.</returns>
- ''' <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
- ''' could not be retrieved.</exception>
- Public Shared Function GetPath(knownFolder As KnownFolder) As String
- Return GetPath(knownFolder, False)
- End Function
- ''' <summary>
- ''' Gets the current path to the specified known folder as currently configured. This does
- ''' not require the folder to be existent.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which current path will be returned.</param>
- ''' <param name="defaultUser">Specifies if the paths of the default user (user profile
- ''' template) will be used. This requires administrative rights.</param>
- ''' <returns>The default path of the known folder.</returns>
- ''' <exception cref="ExternalException">Thrown if the path
- ''' could not be retrieved.</exception>
- Public Shared Function GetPath(knownFolder As KnownFolder, defaultUser As Boolean) As String
- Return GetPath(knownFolder, KnownFolderFlags.DontVerify, defaultUser)
- End Function
- ''' <summary>
- ''' Gets the default path to the specified known folder. This does not require the folder
- ''' to be existent.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which default path will be returned.</param>
- ''' <returns>The current (and possibly redirected) path of the known folder.</returns>
- ''' <exception cref="ExternalException">Thrown if the path
- ''' could not be retrieved.</exception>
- Public Shared Function GetDefaultPath(knownFolder As KnownFolder) As String
- Return GetDefaultPath(knownFolder, False)
- End Function
- ''' <summary>
- ''' Gets the default path to the specified known folder. This does not require the folder
- ''' to be existent.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which default path will be returned.</param>
- ''' <param name="defaultUser">Specifies if the paths of the default user (user profile
- ''' template) will be used. This requires administrative rights.</param>
- ''' <returns>The current (and possibly redirected) path of the known folder.</returns>
- ''' <exception cref="ExternalException">Thrown if the path
- ''' could not be retrieved.</exception>
- Public Shared Function GetDefaultPath(knownFolder As KnownFolder, defaultUser As Boolean) As String
- Return GetPath(knownFolder, KnownFolderFlags.DefaultPath Or KnownFolderFlags.DontVerify, defaultUser)
- End Function
- ''' <summary>
- ''' Creates and initializes the known folder.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which will be initialized.</param>
- ''' <exception cref="ExternalException">Thrown if the known
- ''' folder could not be initialized.</exception>
- Public Shared Sub Initialize(knownFolder As KnownFolder)
- Initialize(knownFolder, False)
- End Sub
- ''' <summary>
- ''' Creates and initializes the known folder.
- ''' </summary>
- ''' <param name="knownFolder">The known folder which will be initialized.</param>
- ''' <param name="defaultUser">Specifies if the paths of the default user (user profile
- ''' template) will be used. This requires administrative rights.</param>
- ''' <exception cref="ExternalException">Thrown if the known
- ''' folder could not be initialized.</exception>
- Public Shared Sub Initialize(knownFolder As KnownFolder, defaultUser As Boolean)
- GetPath(knownFolder, KnownFolderFlags.Create Or KnownFolderFlags.Init, defaultUser)
- End Sub
- Private Shared Function GetPath(knownFolder As KnownFolder, flags As KnownFolderFlags, defaultUser As Boolean) As String
- Dim outPath As IntPtr
- Dim result As Integer = SHGetKnownFolderPath(New Guid(FolderGUIDs(CInt(knownFolder))), CUInt(flags), New IntPtr(If(defaultUser, -1, 0)), outPath)
- If result >= 0 Then
- Return Marshal.PtrToStringUni(outPath)
- Else
- Throw New ExternalException("Unable to retrieve the known folder path. It may not " + "be available on this system.", result)
- End If
- Marshal.FreeCoTaskMem(outPath)
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment