'Add to Favorites' dialog : Favorites Dialog « Windows « VB.Net Tutorial






Imports System.Runtime.InteropServices


public class AddToFavoritesDialog
    Public Shared Sub Main

        Dim szTitle As String
        Dim dwReturn As Integer
        Dim pidl As Integer
        Dim szPath As String
        szTitle = "your new Favorites"
    
        szPath = "www.java2s.com"
    
        dwReturn = SHGetSpecialFolderLocation(0, CSIDL_FAVORITES, pidl)
    
        If dwReturn = 0 Then
          dwReturn = DoAddToFavDlg(0, szPath, Len(szPath), szTitle, Len(szTitle), pidl)
    
          If dwReturn = 1 Then
            szPath = szPath.Substring(0, szPath.IndexOf(vbNullChar))
    
            WritePrivateProfileString("InternetShortcut", "URL", "www.java2s.com", szPath)
          End If
        End If
    
    End Sub

End Class



Module Module1
  Public Const CSIDL_PROGRAMS As Short = &H2S
  ' My Documents
  Public Const CSIDL_PERSONAL As Short = &H5S
  Public Const CSIDL_FAVORITES As Short = &H6S
  Public Const CSIDL_STARTUP As Short = &H7S
  Public Const CSIDL_STARTMENU As Short = &HBS
  Public Const CSIDL_DESKTOPDIRECTORY As Short = &H10S
  ' {Windows}\ShellNew
  Public Const CSIDL_TEMPLATES As Short = &H15S
  Public Const CSIDL_COMMON_STARTMENU As Short = &H16S
  Public Const CSIDL_COMMON_PROGRAMS As Short = &H17S
  Public Const CSIDL_COMMON_STARTUP As Short = &H18S
  Public Const CSIDL_COMMON_DESKTOPDIRECTORY As Short = &H19S
  Public Const CSIDL_APPDATA As Short = &H1AS
  Public Const CSIDL_COMMON_FAVORITES As Short = &H1FS
  ' All Users\Application Data Windows NT
  Public Const CSIDL_COMMON_APPDATA As Short = &H23S

  ' nShowCmd
  Public Const SW_HIDE As Short = 0
  Public Const SW_SHOWNORMAL As Short = 1
  Public Const SW_SHOWMINIMIZED As Short = 2
  Public Const SW_SHOWMAXIMIZED As Short = 3
  Public Const SW_MAXIMIZE As Short = 3
  Public Const SW_SHOWNOACTIVATE As Short = 4
  Public Const SW_SHOW As Short = 5
  Public Const SW_MINIMIZE As Short = 6
  Public Const SW_SHOWMINNOACTIVE As Short = 7
  Public Const SW_SHOWNA As Short = 8
  Public Const SW_RESTORE As Short = 9

  ' Error Code
  Public Const ERROR_FILE_NOT_FOUND As Short = 2
  Public Const ERROR_PATH_NOT_FOUND As Short = 3
  Public Const ERROR_BAD_FORMAT As Short = 11

  Public Const SE_ERR_FNF As Short = 2
  Public Const SE_ERR_PNF As Short = 3
  Public Const SE_ERR_ACCESSDENIED As Short = 5
  Public Const SE_ERR_OOM As Short = 8
  Public Const SE_ERR_SHARE As Short = 26
  Public Const SE_ERR_ASSOCINCOMPLETE As Short = 27
  Public Const SE_ERR_DDETIMEOUT As Short = 28
  Public Const SE_ERR_DDEFAIL As Short = 29
  Public Const SE_ERR_DDEBUSY As Short = 30
  Public Const SE_ERR_NOASSOC As Short = 31
  Public Const SE_ERR_DLLNOTFOUND As Short = 32

  Public Declare Function DoAddToFavDlg Lib "shdocvw.dll" (ByVal hwnd As Integer, ByVal szPath As String, ByVal nSizeOfPath As Integer, ByVal szTitle As String, ByVal nSizeOfTitle As Integer, ByVal pidl As Integer) As Integer

  Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByRef pidl As Integer) As Integer

  Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
End Module








24.13.Favorites Dialog
24.13.1.'Add to Favorites' dialog
24.13.2.Organize FavoritesOrganize Favorites