CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2000
    Location
    Netherlands
    Posts
    5

    Creating Shortcuts

    I've found lotsa code about creating shortcuts but I'm missing one thing in these codes; how to set the 'Start In' folder property.
    Does anybody have an idea ???

    Thanx,
    Erik


  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Creating Shortcuts

    (Taken from my article at : http://codeguru.developer.com/vb/articles/1923.shtml )

    If you are using VB6 (or have installed the Windows Scripting Host), creating desktop shortcuts is easy :


    '
    ' Make sure that you are referencing the
    ' windows scripting host : WSHOM.OCX
    '
    Dim oShell as IWshShell_Class ' our shell object
    Dim oShortcut as IWshShortcut_Class ' our ShortCut object
    Dim sDesktop as string ' path of desktop
    Dim sTargetPath as string ' path to notepad.exe
    '
    on error GoTo vbErrorHandler
    '
    set oShell = new IWshShell_Class ' Create shell object
    '
    ' get desktop path
    '
    sDesktop = oShell.SpecialFolders.Item("Desktop")
    '
    ' Create shortcut on desktop
    '
    set oShortcut = oShell.CreateShortcut(sDesktop & "\npad.lnk")

    '
    ' Now fill in the shortcut properties
    '

    With oShortcut
    sTargetPath = oShell.ExpandEnvironmentStrings("%windir%")
    .TargetPath = sTargetPath & "\notepad.exe" ' change this to whatever you like
    .Arguments = "" ' ditto again !
    .WorkingDirectory = sDesktop ' as above!
    .Hotkey = "Ctrl + Alt + A"
    .Save
    End With
    Exit Sub
    '
    vbErrorHandler:
    MsgBox Err.Description, , "Create Shortcut"




    The '.WorkingDirectory' is the same as 'Start Location' - the scripting host has tonnes of other useful features too.



    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  3. #3
    Guest

    Re: Creating Shortcuts

    ' i just cuted an pasted from my shortcut creator thing
    Dim i As Integer
    Dim lResult As Long
    Dim lpil As Long
    Dim sFilePath As String
    Dim sFileName As String
    Dim sRecentPath As String
    Dim sDesktopPath As String
    Dim sFilePathOld As String
    Dim sFilePathNew As String
    Dim sShortCutName As String
    Dim sMsg As String
    Dim SHFileOp As SHFILEOPSTRUCT
    '
    ' Add a shortcut to the Windows desktop.
    '
    If Trim$(txtApp) = "" Then Exit Sub
    If Trim$(txtName) = "" Then Exit Sub

    On Error GoTo cmdCreateError
    Screen.MousePointer = vbHourglass
    sFilePath = Trim$(txtApp)
    sShortCutName = Trim$(txtName) & ".lnk"
    '
    ' Get the paths of the folders to add the shortcuts to.
    ' The folders are the Recent Files List and the Desktop.
    '
    sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    sDesktopPath = fGetSpecialFolder(CSIDL_DESKTOPDIRECTORY)
    sMsg = "Error retrieving folder location."
    If sRecentPath <> "" And sDesktopPath <> "" Then
    '
    ' Create a shortcut in the Recent Files list. The
    ' SHARD_PATH flag says that the following parameter
    ' is a path.
    '
    sMsg = "Error adding shortcut to the Recent File list."
    lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    Sleep (1500)
    If lResult Then
    '
    ' Extract the .exe name from the path.
    '
    i = 1
    sFileName = sFilePath
    Do While i
    i = InStr(1, sFileName, "\")
    If i Then sFileName = Mid$(sFileName, i + 1)
    Loop
    '
    ' Move the shortcut from the Recent folder to the Desktop.
    ' Since the shortcut now resides in the Recent folder,
    ' modify the file name to include the Recent folder
    ' path. Also, append ".lnk" to the original filename.
    '
    sFilePath = sRecentPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    With SHFileOp
    .wFunc = FO_MOVE
    .pFrom = sFilePath
    .pTo = sDesktopPath
    .fFlags = FOF_SILENT
    End With

    sMsg = "Error creating desktop shortcut."
    lResult = SHFileOperation(SHFileOp)
    Sleep (1500)
    If lResult = 0 Then
    '
    ' Rename the link.
    '
    sFilePathOld = sDesktopPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    sFilePathNew = sDesktopPath & sShortCutName & vbNullChar & vbNullChar
    With SHFileOp
    .wFunc = FO_RENAME
    .pFrom = sFilePathOld
    .pTo = sFilePathNew
    .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    End With
    sMsg = "Error renaming desktop shortcut."
    lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    sMsg = ""
    '
    ' Refresh the desktop to display the shortcut.
    '
    Call SHGetSpecialFolderLocationD(Me.hwnd, CSIDL_DESKTOP, lpil)
    Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    End If
    End If
    End If

    Screen.MousePointer = vbDefault
    Exit Sub

    cmdCreateError:
    MsgBox "Error creating desktop shortcut. " & sMsg, vbExclamation, "Create Shortcut"



  4. #4
    Join Date
    Jan 2000
    Location
    Netherlands
    Posts
    5

    Re: Creating Shortcuts

    It seems to me that it isn't possible to set the 'Start In' folder with this code. So I prefer the code posted by Chris but thanx anyway

    Erik


  5. #5
    Join Date
    Jan 2000
    Location
    Netherlands
    Posts
    5

    Re: Creating Shortcuts

    Works great !! Thanx


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured