הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, August 18, 2018 2:06 AM
I have a folder with shortcuts that link to music files (*.mp3). I want the user to be able to select a shortcut, and then play the music file that it is linked to.
Robert Homes
All replies (23)
Saturday, August 18, 2018 7:01 AM ✅Answered | 2 votes
Change the target path to the shortcut .lnk file. The function returns the shortcut target path.
'how to get shortcut target file from shortcut .lnk file
Imports System.IO
Imports Shell32
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'add the proper link here
Dim target As String = "C:\Users\your user name\Desktop\DirectX.lnk"
Dim targetfile As String = GetShortcutTargetFile(target)
MsgBox(targetfile)
End Sub
Public Shared Function GetShortcutTargetFile(ByVal shortcutFilename As String) As String
Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename)
Dim filenameOnly As String = Path.GetFileName(shortcutFilename)
Dim shell As Shell = New Shell()
Dim folder As Folder = shell.[NameSpace](pathOnly)
Dim folderItem As FolderItem = folder.ParseName(filenameOnly)
If folderItem IsNot Nothing Then
Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject)
Return link.Path
End If
Return String.Empty
End Function
End Class
Sunday, August 19, 2018 11:54 AM ✅Answered | 2 votes
You can also get a shortcut's target path by adding a reference to Windows Script Host Object Model as shown here...
Then you can get the target path as shown here...
Imports IWshRuntimeLibrary
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using ofd As New OpenFileDialog
ofd.Filter = "Shortcut Files (*.lnk)|*.lnk"
If ofd.ShowDialog = DialogResult.OK Then
Dim target As String = GetTargetPath(ofd.FileName)
AxWindowsMediaPlayer1.URL = target
Me.Text = IO.Path.GetFileName(target)
End If
End Using
End Sub
Private Function GetTargetPath(LinkFilename As String) As String
Dim scpath As String = ""
If LinkFilename.ToLower.EndsWith(".lnk") Then
Dim theShell As New WshShell()
Dim theShortcut As WshShortcut = CType(theShell.CreateShortcut(LinkFilename), WshShortcut)
scpath = theShortcut.TargetPath
System.Runtime.InteropServices.Marshal.ReleaseComObject(theShortcut)
System.Runtime.InteropServices.Marshal.ReleaseComObject(theShell)
End If
Return scpath
End Function
End Class
This is using the Shell to get the target path just like Tom's example, but uses a different assembly to access it. 8)
If you say it can`t be done then i`ll try it
Monday, September 3, 2018 4:09 PM ✅Answered | 1 vote
Tommy:
Well, I took more than an hour! - about 2 weeks, but I do feel better now and ready to jump back ino this. Thanks for sticking with me!
I took your code, changed it to a form like you said, and put the form in a new vb project. Then I created a shortcut in one of my Music folders, and put the shortcut name in your code. Here's what I got:
Imports System.IO Imports Shell32 Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'add the proper link here Dim target As String = "X:\Music\Lucinda Williams\Ramblin'\06 - Jug Band Music.mp3 - Shortcut" Dim targetfile As String = GetShortcutTargetFile(target) MsgBox(targetfile) End Sub Public Shared Function GetShortcutTargetFile(ByVal shortcutFilename As String) As String Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename) Dim filenameOnly As String = Path.GetFileName(shortcutFilename) Dim shell As Shell = New Shell() Dim folder As Folder = shell.[NameSpace](pathOnly) Dim folderItem As FolderItem = folder.ParseName(filenameOnly) If folderItem IsNot Nothing Then Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject) Return link.Path End If Return String.Empty End Function End Class
I added the Shell think in References and the code looked okay, no design time errors. Then I ran the program, and this is what I got:
In other words, it didn't work! Can you see what I did wrong? Thanks.
Robert Homes
LOL !
Shortcuts have the extension **.LNK ** so you need to add that to your target path as shown.
You dont see the extension using windows file explorer etc as the system is set to not show the extension.
Thats
** .lnk**
** period ell enn kay**
Dim target As String = "X:\Music\Lucinda Williams\Ramblin'\06 - Jug Band Music.mp3 - Shortcut**.lnk**"
See where I added the .lnk at the end of the target string above?
I think that is all that is wrong.
It is confusing.
Saturday, August 18, 2018 7:37 AM | 1 vote
You can just execute the shortcut :
Dim p1 As Process = System.Diagnostics.Process.Start(New ProcessStartInfo("e:\test\test_shortcut.lnk"))
(the Shell will launch the shortcut target with the associated application)
Saturday, August 18, 2018 3:00 PM
Castorix31 --
I tried this unsuccessfully.
My shortcut appears in the folder as:
Never Going Back Again.mp3
It's target is:
"X:\Music\Lindsey Buckingham\Live at USC (2015)\Never Going Back Again.mp3"
But when it runs in your VB code it shows up as:
X:\Music\Lindsey Buckingham\Live at USC (2015)\Never Going Back Again.lnk
WHAT NOW?
Robert Homes
Saturday, August 18, 2018 3:59 PM
If I copy a shortcut to a MP3 :
"01. IMANY - Don't Be so Shy (Filatov & Karas Remix).mp3"
into
"e:\test"
the shortcut is (french OS) :
"e:\test\01. IMANY - Don't Be so Shy (Filatov & Karas Remix).mp3 - Raccourci.lnk"
("- Raccourci" is added)
and
Dim p1 As Process = System.Diagnostics.Process.Start(New ProcessStartInfo("e:\test\01. IMANY - Don't Be so Shy (Filatov & Karas Remix).mp3 - Raccourci.lnk"))
works for me (Windows 10)
Saturday, August 18, 2018 4:31 PM
Castorix31 --
I always remove the " - Raccourci" in the displayed filename. Maybe your code removes any extension found at the end of the filename before appending the ".lnk" extension. That would explain it.
Robert Homes
Saturday, August 18, 2018 4:52 PM
I can remove "-Raccourci.mp3" which was added
to get
Dim p1 As Process = System.Diagnostics.Process.Start(New ProcessStartInfo("e:\test\01. IMANY - Don't Be so Shy (Filatov & Karas Remix).lnk"))
Saturday, August 18, 2018 7:29 PM
I always remove the " - Raccourci" in the displayed filename.
Robert Homes
How are you removing the " - Raccourci" from the filename? Are you sure you are not removing the ".lnk" from the filename when you do that?
Normally when you load filename(s) for something like this, you would store the full filename in a variable or collection. Then you should only modify the filename that is displayed to the user, not the one you use to execute the shortcut. For example....
Dim FullLinkFilename As String = "C:\Test\05.Saturday Night Special.mp3 - Shortcut.lnk"
Label1.Text = IO.Path.GetFileName(FullLinkFilename.Remove(FullLinkFilename.LastIndexOf(" -"))) 'dislpays '05.Saturday Night Special.mp3' in the label
Process.Start(FullLinkFilename)
Or if using an OpenFileDialog....
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using ofd As New OpenFileDialog
ofd.Filter = "Link Files|*.lnk"
If ofd.ShowDialog = DialogResult.OK Then
Label1.Text = IO.Path.GetFileName(ofd.FileName.Remove(ofd.FileName.LastIndexOf(" -"))) 'dislpays '05.Saturday Night Special.mp3' in the label
Process.Start(ofd.FileName)
End If
End Using
End Sub
OT - Hi Tom. 8)
If you say it can`t be done then i`ll try it
Sunday, August 19, 2018 3:38 AM
Tommy, your code doesn't work for me. I made the GetShortcutTargetFile a function in a module:
Imports System.IO
Imports Shell32
Module modShortTarget
Function GetShortcutTargetFile(ByVal shortcutFilename As String) As String
Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename)
Dim filenameOnly As String = Path.GetFileName(shortcutFilename)
Dim shell As Shell = New Shell()
Dim folder As Folder = shell.[NameSpace](pathOnly)
Dim folderItem As FolderItem = folder.ParseName(filenameOnly)
If folderItem IsNot Nothing Then
Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject)
Return link.Path
Else
Return String.Empty
End If
End Function
End Module
But several errors are highlighted and they all say "not defined". That goes for "Shell", "Folder", "FolderItem" and "ShellLinkObject"
Please help me understand. Thanks.
Robert Homes
Sunday, August 19, 2018 11:34 AM | 2 votes
Tommy, your code doesn't work for me. I made the GetShortcutTargetFile a function in a module:
Imports System.IO Imports Shell32 Module modShortTarget Function GetShortcutTargetFile(ByVal shortcutFilename As String) As String Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename) Dim filenameOnly As String = Path.GetFileName(shortcutFilename) Dim shell As Shell = New Shell() Dim folder As Folder = shell.[NameSpace](pathOnly) Dim folderItem As FolderItem = folder.ParseName(filenameOnly) If folderItem IsNot Nothing Then Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject) Return link.Path Else Return String.Empty End If End Function End Module
But several errors are highlighted and they all say "not defined". That goes for "Shell", "Folder", "FolderItem" and "ShellLinkObject"
Please help me understand. Thanks.
Robert Homes
I suggest you make the simple one form example I showed and get it running first first and then make your changes.
However I made the module you show and it works. I think you just need to add a reference for the shell com as shown (for use in form or module):
PS Hi Razerz!
Sunday, August 19, 2018 12:25 PM
Hi Razerz,
As Robert asks here:
Do you know what is the difference between AX and WMPLib for media player?
Hi Tom,
No, not right off hand I don't. I would have to look further into it but, I have to take off for work in a few minutes so I don't have time to look now. All I know about the WMPLib right now is that it is added as a reference to your project when you add an AxWindowsMediaPlayer to the form, and that it has enumerations and stuff that are used by the AxWindowsMediaPlayer. 8)
If you say it can`t be done then i`ll try it
Sunday, August 19, 2018 2:56 PM
Tommy
I can't get the Shell version working. I think that's due to the [NameSpace] word in there. So I tried using the Wsh thing instead, but can't get it working either. When it hits the "Dim theShortcut as WshShortcut" line it doesn't assign a good value to "theShortcut" and then the "scpath = " line gives that variable an empty string.
What now?
Robert Homes
Sunday, August 19, 2018 3:07 PM | 1 vote
Tommy
I can't get the Shell version working. I think that's due to the [NameSpace] word in there. So I tried using the Wsh thing instead, but can't get it working either. When it hits the "Dim theShortcut as WshShortcut" line it doesn't assign a good value to "theShortcut" and then the "scpath = " line gives that variable an empty string.
What now?
Robert Homes
Yeah. I think windows file explorer etc wont show you the name and extension depending on your system settings.
What I did was rmb click the .mp3 or whatever file, then select create shortcut from the popup menu.
Then as I recall the name was myfile - shortcut (like Castor etc) and I copied that name to vb and added .lnk so now the filename with extension is "myfile - shortcut.lnk" in vb and that works.
I think all your problems are you are not using the correct name and extension and I don't know exactly what the rules are. Just add it to the end. Name the file manually if you must.
It may be it wont work if you don't use .lnk in the vb part.
PS In windows file explorer, rmb click the filename and then select properties from the popup menu, then to details tab, it shows the full name and extension for the shortcut.
PS Oh, well now I am combining your two threads in my mind and so I am not sure what we are doing exactly. But I think the answer to both is use the filename.lnk.
You may need to update your example again to exactly what you are doing now.
You should be able to get my example from the first post to work. Put a break on the function line, single step execution to though the function code and then look at all the file name variables and make sure you understand how it works. Then move to your large application.
Sunday, August 19, 2018 7:10 PM
Tommy
.... So I tried using the Wsh thing instead, but can't get it working either. When it hits the "Dim theShortcut as WshShortcut" line it doesn't assign a good value to "theShortcut" and then the "scpath = " line gives that variable an empty string.
What now?
Robert Homes
Did you try my code example in a new form project and use my exact code? It should work.
If the GetTargetPath function in my code example returns an empty string, it tells me that the filename you are passing to the function must not have a (.lnk) extension on it. If you are modifying the shortcut (.lnk) filename somehow, then don't, just pass the full file path, including the (.lnk) extension.
This shows my example working...
If you say it can`t be done then i`ll try it
Sunday, August 19, 2018 9:01 PM
Tom & Razerz:
I think that the Ax stuff is unnecessary if you are using WMP invisibly. The Ax stuff seems to be needed only if you want to display the WMP and its controls. Since I don't, I've finally weeded out all the stuff I put in my code referring to Ax, and it seems to work just fine.
Robert Homes
Sunday, August 19, 2018 9:11 PM
Razers:
I am very confused. I think I've tried everything you suggested, but still can't get it working.
A big problem for me is that there are TWO extensions for a shortcut file, one visible and the other hidden. From what I can tell, a shortcut file ALWAYS has a hidden extension of ".LNK", whereas the visible extension can actually be anything. When I create a shortcut, the visible extension seems to always be set to the extension of the target file; you can change that to anything, since the visible extension doesn't affect anything.
OR DOES IT?? -- I've been told to change the extension to ".LNK". Whoever tells me this, are they referring to the visible extension or the hidden one? For me to understand correctly any discussion about all this, I would have to know what extension people are referring to -- the hidden one or the visible one?
Also, what extension (hidden or visible) does VB return from such functions as Path.GetExtension? or Path.GetFilename?
Like I said, I am CONFUSED! One thing for sure, I can't get either the Shell or the WSH things working, and I think I have tried everything you guys have thrown at me. (Of course, I have to admit that I must be doing something wrong on my end. BUT WHAT??I
Also, what extension (visible or hidden) do Windows (Shell?) commands look for? I think they use the visible one. But I think VB uses the hidden one. What a mess!
Robert Homes
Sunday, August 19, 2018 9:46 PM
Razers:
I am very confused. I think I've tried everything you suggested, but still can't get it working.
A big problem for me is that there are TWO extensions for a shortcut file, one visible and the other hidden. From what I can tell, a shortcut file ALWAYS has a hidden extension of ".LNK", whereas the visible extension can actually be anything. When I create a shortcut, the visible extension seems to always be set to the extension of the target file; you can change that to anything, since the visible extension doesn't affect anything.
OR DOES IT?? -- I've been told to change the extension to ".LNK". Whoever tells me this, are they referring to the visible extension or the hidden one? For me to understand correctly any discussion about all this, I would have to know what extension people are referring to -- the hidden one or the visible one?
Also, what extension (hidden or visible) does VB return from such functions as Path.GetExtension? or Path.GetFilename?
Like I said, I am CONFUSED! One thing for sure, I can't get either the Shell or the WSH things working, and I think I have tried everything you guys have thrown at me. (Of course, I have to admit that I must be doing something wrong on my end. BUT WHAT??I
Also, what extension (visible or hidden) do Windows (Shell?) commands look for? I think they use the visible one. But I think VB uses the hidden one. What a mess!
Robert Homes
So... are you saying that you have tried my full example code exactly as I posted it, in a new form project and it did not work?
If my example works as it is, and the problem is encountered when you try implementing my code it into your application, then you will need to post your code so we can see what may be causing the problem.
We also really need to know how you are getting the filename(s) of the shortcut files.
###############
If you have the explorer settings in your OS set to hide extensions, then you will not see the (.lnk) extension at the end of the shortcut's filename in the explorer window. The only extension you would have left to see is maybe the (.mp3) extension which would be part of the filename for the (.lnk) file. For example, this is what I see in explorer...
05.Saturday Night Special.mp3 - Shortcut
But, the shortcut file extension is hidden. So, the real filename is actually...
05.Saturday Night Special.mp3 - Shortcut.lnk
In the above filename, the (.mp3) is just part of the shortcut's filename.
If you say it can`t be done then i`ll try it
Sunday, August 19, 2018 11:21 PM | 1 vote
Tom & Razerz:
I think that the Ax stuff is unnecessary if you are using WMP invisibly. The Ax stuff seems to be needed only if you want to display the WMP and its controls. Since I don't, I've finally weeded out all the stuff I put in my code referring to Ax, and it seems to work just fine.
Robert Homes
Me thinks you make it too hard.
We gave you lots of differenct ways to do it so it got confusing.
I will repeat what Razerz showed as I wrote it:
The file extension is the last three chars after the last dot.The filename is just a string. The complete path to the file again a string. You just put the characters together in some patttern or not. The extension is just a convention. There is no "hidden" extension. Just that windows does not always show it as Razerz shows. Shortcuts have extensions .lnk. Look at the file properties - detail for the complete name.
Some things use the extension to determine what type file or action to take etc. You can have many kinds of names they are only names made of strings. The extension is used to sort what type of file ie jpg is an image, .wav is sound, mp3 is sound etc. Some applications may or may not use the extension for anything other than the name. Some may require it a certain way.
I guess you know you can just make a list of the .mp3 file locations and then play the mp3 files selected from the list? You dont need a folder of shortcuts.
What are you using the shortcut for? You dont need them unless you have some purpose for it??? What are you making anyway?
Edit: moved player example to this thread.
Monday, August 20, 2018 12:27 AM
Tommy & Razers:
Listen, I don't think I'm up to this. I confused and getting confusedier at every step.
Tommy, I can't even figure out how to create a project consisting of the code you gave me at the start of this thing. It's a "Class", so what kind of "Project" must I create? And I don't know how the form part of your code gets mixed with the function part. I'm just lost from the get-go.
This is why I changed your stuff to a Module, something I can better understand. And I dropped the form thing so I could call the function from another part of my existing code.
Here is a copy of what I came up with:
Imports System.IO
Imports Shell32
Imports IWshRuntimeLibrary
Module modShortTarget
''using MicrosoftShellControlsAndAutomation
'Function GetShortcutTargetPath(ByVal shortcutFilename As String) As String
' Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename)
' Dim filenameOnly As String = Path.GetFileName(shortcutFilename)
' Dim shell As Shell = New Shell()
' Dim folder As Folder = shell.[NameSpace](pathOnly)
' Dim folderItem As FolderItem = folder.ParseName(filenameOnly)
' If folderItem IsNot Nothing Then
' Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject)
' Return link.Path
' Else
' Return String.Empty
' End If
'End Function
'using WindowsScriptHostObjectModel
Function GetShortcutTargetPath(LinkFilename As String) As String
Dim scpath As String = ""
If LinkFilename.ToLower.EndsWith(".lnk") Then
Dim theShell As New WshShell()
Dim theShortcut As WshShortcut = CType(theShell.CreateShortcut(LinkFilename), WshShortcut)
scpath = theShortcut.TargetPath
System.Runtime.InteropServices.Marshal.ReleaseComObject(theShortcut)
System.Runtime.InteropServices.Marshal.ReleaseComObject(theShell)
End If
Return scpath
End Function
End Module
I included both of your functions, the "shell" thing and the "wsh" thing so I could try them alternately. To try one, I just comment out the other one.
You ask me where am I getting the shortcut files? Okay, here goes:
I have a collection of music files in X:\Music. I go there and select a file, right-click it and select "Create Shortcut". I then copy that shortcut into the folder my program will look for itl, which is: C:\Users\Robert\Documents\BriefCase\Favorites\Music.
Now the user selects one of the shortcut files, and the program tries to get the full pathname of the target file for that shortcut and then plays that target file in Windows Media Player.
You may be wondering how the user will know where MY original music files are. Well, he doesn't have to know. All he has to do is go to wherever HIS music files are on his computer, make a shortcut for one, then copy the shortcut to his Documents\BriefCase\Favorites\Music folder. My program also includes a routine to let the User browse to his chosen music folder and select a file there (using an OpenFile Dialog), then the program automatically creates the shortcut and moves it into Documents\BriefCase\Favorites\Music.
Anyway, I think I've bothered you two pros enough with this. If you want to just drop it, I won't complain. Frankly, I'm ready to call it quits, if you all want to -- unless you can find something in my code that I did wrong. Thanks for trying to help me!
Robert Homes
Monday, August 20, 2018 12:47 AM | 1 vote
Tommy & Razers:
Listen, I don't think I'm up to this. I confused and getting confusedier at every step.
Tommy, I can't even figure out how to create a project consisting of the code you gave me at the start of this thing. It's a "Class", so what kind of "Project" must I create? And I don't know how the form part of your code gets mixed with the function part. I'm just lost from the get-go.
Oh I see.
Public Class FileGetSortcutTargetLink
Change FileGetSortcutTargetLink to Form1 in a default windows forms project you start in visual studio. ie
Public Class Form1
'copy and paste the rest of the code
' change to the form name that you want to use in your project
End Class
No its not a bother as long as you are following. You need to get into the forum system of you show what you have done when you have a problem and describe the problem so it can be reproduced. We try to help.
I have not read the rest of your post yet.
Just change my example to form1 one in a one form project.
You should be able to run Razerz too.
So just pick one example and get it working. Don't do anything else. Its a basic thing you need to do to use visual studio and vb. Try just one step at a time as too many errs at once makes it frustrating.
Its easier to do it all in one form than to use a module. So Stop it! Do it how we tell you!
:)
PS I changed the first example code above so now it is Form1.
Take a break. An hour or look in the morning. It all will look better.
Monday, September 3, 2018 3:13 PM
Tommy:
Well, I took more than an hour! - about 2 weeks, but I do feel better now and ready to jump back ino this. Thanks for sticking with me!
I took your code, changed it to a form like you said, and put the form in a new vb project. Then I created a shortcut in one of my Music folders, and put the shortcut name in your code. Here's what I got:
Imports System.IO
Imports Shell32
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'add the proper link here
Dim target As String = "X:\Music\Lucinda Williams\Ramblin'\06 - Jug Band Music.mp3 - Shortcut"
Dim targetfile As String = GetShortcutTargetFile(target)
MsgBox(targetfile)
End Sub
Public Shared Function GetShortcutTargetFile(ByVal shortcutFilename As String) As String
Dim pathOnly As String = Path.GetDirectoryName(shortcutFilename)
Dim filenameOnly As String = Path.GetFileName(shortcutFilename)
Dim shell As Shell = New Shell()
Dim folder As Folder = shell.[NameSpace](pathOnly)
Dim folderItem As FolderItem = folder.ParseName(filenameOnly)
If folderItem IsNot Nothing Then
Dim link As ShellLinkObject = CType(folderItem.GetLink, ShellLinkObject)
Return link.Path
End If
Return String.Empty
End Function
End Class
I added the Shell think in References and the code looked okay, no design time errors. Then I ran the program, and this is what I got:
In other words, it didn't work! Can you see what I did wrong? Thanks.
Robert Homes
Monday, September 3, 2018 9:05 PM
Yes, Tommy, it sure is confusing. Anyway, I finally got it working - WITH YOUR HELP. Thanks.
Now I have a related question, maybe you can help with that -- how to you CREATE a shortcut (to a file on disk) using vb code, and with the Shell stuff (not the WSH stuff). Since I'm getting the target files with the Shell stuff, I thought I should use the same stuff to create the shortcuts too.
I decided to ask this in a separate question, which I just posted. This thread has gotten way too long!
Robert Homes