Results 1 to 7 of 7

Thread: Highlight a filename in the current directory (explorer window)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    160

    Highlight a filename in the current directory (explorer window)

    Hi

    I'm looking to copy a file to the current directory open in an explorer window (I'm going to get the program to launch from the right button context menu).

    I've got the file to copy over, but I need to be able to highlight/select it - the same as if the user had clicked on it.

    Essentially I need a bit of code I can pass 'path/file 3.txt' to which will take me from this:





    to this:





    Something similar was done in this post, but I need to work with the current explorer window, not open a new one.

    Is this possible?

    Thanks for reading!
    Last edited by Precision999; Mar 29th, 2019 at 07:49 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Highlight a filename in the current directory (explorer window)

    Try this...

    Code:
    Process.Start("explorer.exe", "/select," & "PATH_TO_FILETOSELECT")

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Highlight a filename in the current directory (explorer window)

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    Process.Start("explorer.exe", "/select," & "PATH_TO_FILETOSELECT")
    Doesn't that start a new explorer, which he stated he didn't want.
    Quote Originally Posted by Precision999 View Post
    ...
    Something similar was done in this post, but I need to work with the current explorer window, not open a new one.

    Is this possible?
    ...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Highlight a filename in the current directory (explorer window)

    Quote Originally Posted by passel View Post
    Doesn't that start a new explorer, which he stated he didn't want.
    I noticed that after i posted. I had a look in spy++. Don't think that's possible that way. Doesn't classic vb have a curdir method? Is there something similar for setting the current file?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Highlight a filename in the current directory (explorer window)

    Might be possible with SendKeys...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    160

    Re: Highlight a filename in the current directory (explorer window)

    Thanks for replies guys.

    I do indeed need this to work with the current window rather then opening a new one. I'm not sure how Sendkeys would help, but from experience I'm sure that using it would bite me in the bum at some point or another!

    Maybe I need to get the name of the window I'm in - but that still leaves how to 'select' the file - I've no idea how to do that.

    Would a different copy method help? - maybe there's a way to do it which leaves the file selected as part of it's process?

    Currently I'm just using:

    Code:
    mydir = Directory.GetCurrentDirectory()
    
    My.Computer.FileSystem.CopyFile(<filename and path to copy>, mydir & "\<name of destination file>")

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Highlight a filename in the current directory (explorer window)

    hi,

    this will return what is selected in the "current" Explorer, see if this helps
    the selected Files I added to a Listbox

    Code:
    Option Strict On
    
    'Add Ref. COM  :
    '1)Microsoft Shell Controls
    '2)Microsoft Shell Controls and Automation
    
    Imports Shell32
    Imports SHDocVw
    
    Public Class Form1
    
        Private Function GetExplorerSelectedFiles() As String()
            Dim ExplorerFiles As New List(Of String)
            Dim exShell As New Shell
            For Each window As ShellBrowserWindow In DirectCast(exShell.Windows, IShellWindows)
                ' check both for either interface to work
                '    add an Exit for to just work the first explorer window 
                If TryCast(window.Document, IShellFolderViewDual) IsNot Nothing Then
                    For Each fi As FolderItem In DirectCast(window.Document, IShellFolderViewDual).SelectedItems
                        ExplorerFiles.Add(fi.Path)
                    Next
    
                ElseIf TryCast(window.Document, ShellFolderView) IsNot Nothing Then
                    For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
                        ExplorerFiles.Add(fi.Path)
                    Next
    
                End If
            Next
    
            Return ExplorerFiles.ToArray
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim files = GetExplorerSelectedFiles()
            ListBox1.Items.AddRange(files)
        End Sub
    End Class
    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

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