15

I have an application which has an option to show the selected file in the folder in which the file resides. My question is, how do I achieve this?

To clarify, if a user in my program selected the "Test.txt" file, then I want a Windows Explorer window to pop up and highlight the file the user selected. You can see similar behavior in LimeWire and uTorrent. If you select a file in either of those programs and choose "Show in Folder", it pops up a Windows Explorer window with the file highlighted and selected. I am trying to duplicate this behavior.

I tried using the following line:

System.Diagnostics.Process.Start("Explorer");

This will popup the Windows Explorer window, however, it always seems to open up by default in "My Documents" folder.

Abbas
  • 6,720
  • 4
  • 35
  • 49
Icemanind
  • 47,519
  • 50
  • 171
  • 296

5 Answers5

29

Here you go,

string fileToSelect = @"C:\temp.img";
string args = string.Format("/Select, \"{0}\"", fileToSelect);

ProcessStartInfo pfi = new ProcessStartInfo("Explorer.exe", args);
System.Diagnostics.Process.Start(pfi);

Note: Adding \" before and after the {0} parameter enables the fileToSelect string to contain spaces (i.e. "C:\My Documents").

From this Thread:
Programmatically select multiple files in windows explorer

Cheers,

Community
  • 1
  • 1
Karthik Mahalingam
  • 1,071
  • 8
  • 10
3

There is a documented API to do this: SHOpenFolderAndSelectItems. Who knows, it might even do the right thing when explorer is not the default shell :)

VB example as requested:

Imports System

Partial Public Class NativeMethods
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ILCreateFromPathW")> _
    Public Shared Function ILCreateFromPathW(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal Path As String) As System.IntPtr
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ILFree")> _
    Public Shared Sub ILFree(<System.Runtime.InteropServices.InAttribute()> ByVal pidl As System.IntPtr)
    End Sub
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ILClone")> _
    Public Shared Function ILClone(<System.Runtime.InteropServices.InAttribute()> ByVal pidl As System.IntPtr) As System.IntPtr
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ILFindLastID")> _
    Public Shared Function ILFindLastID(<System.Runtime.InteropServices.InAttribute()> ByVal pidl As System.IntPtr) As System.IntPtr
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ILRemoveLastID")> _
    Public Shared Function ILRemoveLastID(<System.Runtime.InteropServices.InAttribute()> ByVal pidl As System.IntPtr) As System.Int32
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="SHOpenFolderAndSelectItems")> _
    Public Shared Function SHOpenFolderAndSelectItems(<System.Runtime.InteropServices.InAttribute()> ByVal pidl As System.IntPtr, ByVal cidl As System.Int32, <System.Runtime.InteropServices.InAttribute()> ByRef child As System.IntPtr, ByVal Flags As System.Int32) As System.Int32
    End Function
End Class

Module Program
    Sub Main()
        Dim pidl, clone, child As System.IntPtr
        pidl = NativeMethods.ILCreateFromPathW("c:\windows\explorer.exe")
        If pidl <> System.IntPtr.Zero Then
            clone = NativeMethods.ILClone(pidl)
            child = NativeMethods.ILFindLastID(clone)
            NativeMethods.ILRemoveLastID(pidl)
            NativeMethods.SHOpenFolderAndSelectItems(pidl, 1, child, 0)
            NativeMethods.ILFree(clone)
            NativeMethods.ILFree(pidl)
        End If
    End Sub
End Module
Anders
  • 97,548
  • 12
  • 110
  • 164
  • This is a link only answer. Please include an example of how to call this from VB.NET – HackSlash Aug 26 '22 at 21:11
  • 1
    @HackSlash It is also a 10 year old answer when the "rules" where different. Also, the question was for c# but I'm feeling generous (keep in mind, I have not coded in VB in years). – Anders Aug 26 '22 at 22:05
  • Wow! Thanks. That's a lot of native calls! – HackSlash Aug 26 '22 at 22:36
1

For VB:

Dim q as Char = ControlChars.Quote
Dim path As String = q & "D:\examples\test doc.txt" & q
Dim psi as New ProcessStartInfo("Explorer.exe", "/Select, " & path)
Process.Start(psi)

As others have pointed out, paths containing spaces must be enclosed in quotes.

Spiritman
  • 23
  • 6
0

You could construct the folder path in a string, then send it to the windows command line to browse.

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx

MAW74656
  • 3,449
  • 21
  • 71
  • 118
0

Strictly to launch Windows Explorer within a specific path, you initial solution works well with C# (VS2010, VS2019), just need one more argument: so, instead of

    System.Diagnostics.Process.Start("Explorer");

that is going to 'MyDocuments' folder, use

    System.Diagnostics.Process.Start("Explorer", filePath); 

or even

    System.Diagnostics.Process.Start("Explorer", @"C:\Program Files");
OvidiuA
  • 1
  • 1