-1

I was wondering if there is anyway to get the file path of a selected file. I have registered a hotkey in reference to this.

E.g. RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)

Which will do certain actions on pressing ctrl, shift and 2. What i want to do is to get the path of a selected file WITHOUT opening OpenFileDialog

e.g. i select mydoc.doc located on my desktop, press ctrl shift and 2, and it will msgbox out the location of the file.

(meaning i click the file mydoc.doc on my desktop, press my hotkey and get the file location. Is there anyway to do this? (Just like how you would click a file in a folder to copy and paste it to another location, i want to click the file press my hotkey and msgbox out its location))

Is there anyway to do this or any direction anyone can point me in?Because i can't find any API which does this... Thanks!

EDIT:

After reading all the updates and the several links here and there, I started to construct my own function for this, I'm just at the part to determine how many selected icons there are, but i keep getting back 0 icons is there something wrong with what I'm doing?

Public Function getDesktopFiles() As String
    Dim vhandle As IntPtr = FindWindow("Progman", "Program Manager")
    vhandle = FindWindowEx(vhandle, IntPtr.Zero, "SHELLDLL_DefView", vbNull)
    vhandle = FindWindowEx(vhandle, IntPtr.Zero, "SysListView32", "FolderView")
    Dim vItemcount As IntPtr
    vItemcount = SendMessage(vhandle, LVM_GETSELECTEDCOUNT, 0, 0)
    Return vItemcount
End Function
user2587774
  • 125
  • 9
  • Selected file? Where? – rory.ap Jun 21 '15 at 13:22
  • define `selected file` - with no dialog it *cant* be something the user selected; selected *how*??? – Ňɏssa Pøngjǣrdenlarp Jun 21 '15 at 13:22
  • I made it clearer, meaning i click the file mydoc.doc on my desktop, press my hotkey and get the file location. Is there anyway to do this? (Just like how you would click a file in a folder to copy and paste it to another location, i want to click the file press my hotkey and msgbox out its location) – user2587774 Jun 21 '15 at 13:23
  • possible duplicate of [Get selected items of folder with WinAPI](http://stackoverflow.com/questions/3382946/get-selected-items-of-folder-with-winapi) – tjleigh Jun 21 '15 at 14:17
  • read and updated! thanks for pointing me out in the right direction! – user2587774 Jun 21 '15 at 14:32
  • Have you checked the value of `vhandle` at each step? My computer's `Progman` window doesn't have any children. But `SHELLDLL_DefView` is a child of a `WorkerW` window. You might need to search all `WorkerW` children for the `SHELLDLL_DefView` window – tjleigh Jun 22 '15 at 10:48

1 Answers1

0

This question has a link to a Raymond Chen blog post which will give you the API calls you require, you will need to convert it to VB but it is based on COM so should be easy. The other answer to that question gives the C# version of Raymond's code...

Edited to add specifics:
1. Include project references to Microsoft Internet Controls (to get the SHDocVw namespace) and Microsoft Shell Controls and Automation (to get the Shell32 namespace)
2. Use SHDocVw.ShellWindows to get an enumeration of open browser windows.
3. Try to cast each item as a ShellBrowserWindow, and then try to cast the Document property as a Shell32.IShellFolderViewDual2 object.
4. The FocusedItem property gives the selected item.

VB.NET code:

Dim windows As New SHDocVw.ShellWindows
For Each window As Object In windows
    Dim browser As SHDocVw.ShellBrowserWindow = window
    Dim folder As Shell32.IShellFolderViewDual2 = browser.Document
    Console.WriteLine(folder.FocusedItem.Path)
Next
Community
  • 1
  • 1
tjleigh
  • 1,134
  • 1
  • 10
  • 23
  • This is for folders, not the desktop, while parts of the concept are the same, it doesn't work for the desktop... – user2587774 Jun 21 '15 at 15:13
  • I thought the desktop was a folder – tjleigh Jun 21 '15 at 15:54
  • The desktop is a folder, if you access and open it as an explorer. But if you're looking to view it as if you boot your computer screen up, then it displays as a listview. The process from what i gather is to hook the Desktop Listview via GetDesktopWindow and then to SysList32 and etc, but i am somehow unable to hook it properly. There seems to be an additional WorkerW that is present in Windows 8 and 7 computers :( – user2587774 Jun 21 '15 at 16:03
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – DeanOC Jun 22 '15 at 00:56
  • added specifics from the links as per @DeanOC comment (I know it doesn't help user2587774 but did it anyway for future readers) – tjleigh Jun 22 '15 at 08:41