Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a folder in explorer and selecting a file

Tags:

c#

explorer

I'm trying to open a folder in explorer with a file selected.

The following code produces a file not found exception:

System.Diagnostics.Process.Start(
    "explorer.exe /select," 
    + listView1.SelectedItems[0].SubItems[1].Text + "\\" 
    + listView1.SelectedItems[0].Text);

How can I get this command to execute in C#?

like image 476
Michael L Avatar asked Dec 02 '08 16:12

Michael L


People also ask

What are the three ways to open a File Explorer?

There are three ways you can open File Explorer from the Start menu. The first is to click the Start button, and then scroll down the list of apps and click “Windows System.” In the submenu, click “File Explorer” to open it. You can also pin File Explorer to the sidebar above the Start button.

How do I get File Explorer to open to a specific folder?

To open Windows File Explorer to a specific folder, create a shortcut to “%SYSTEMROOT%\explorer.exe /e,<folder>”, where “<folder>” is the folder of your choice.

How do you explore a folder?

Alternatively, right-clicking on a folder (by pressing the right mouse button) highlights it and opens a contextual menu. Click on Open (once, with the left mouse button). If you're using a touchscreen device, press-and-hold the folder on your screen to open a larger version of the same contextual menu.


6 Answers

// suppose that we have a test.txt at E:\
string filePath = @"E:\test.txt";
if (!File.Exists(filePath))
{
    return;
}

// combine the arguments together
// it doesn't matter if there is a space after ','
string argument = "/select, \"" + filePath +"\"";

System.Diagnostics.Process.Start("explorer.exe", argument);
like image 61
Samuel Yang Avatar answered Sep 22 '22 02:09

Samuel Yang


Use this method:

Process.Start(String, String)

First argument is an application (explorer.exe), second method argument are arguments of the application you run.

For example:

in CMD:

explorer.exe -p

in C#:

Process.Start("explorer.exe", "-p")
like image 41
Tom Smykowski Avatar answered Sep 19 '22 02:09

Tom Smykowski


If your path contains comma's, putting quotes around the path will work when using Process.Start(ProcessStartInfo).

It will NOT work when using Process.Start(string, string) however. It seems like Process.Start(string, string) actually removes the quotes inside of your args.

Here is a simple example that works for me.

string p = @"C:\tmp\this path contains spaces, and,commas\target.txt";
string args = string.Format("/e, /select, \"{0}\"", p);

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "explorer";
info.Arguments = args;
Process.Start(info);
like image 35
Jan Croonen Avatar answered Sep 19 '22 02:09

Jan Croonen


Just my 2 cents worth, if your filename contains spaces, ie "c:\My File Contains Spaces.txt", you'll need to surround the filename with quotes otherwise explorer will assume that the othe words are different arguments...

string argument = "/select, \"" + filePath +"\"";
like image 39
Adrian Hum Avatar answered Sep 20 '22 02:09

Adrian Hum


Using Process.Start on explorer.exe with the /select argument oddly only works for paths less than 120 characters long.

I had to use a native windows method to get it to work in all cases:

[DllImport("shell32.dll", SetLastError = true)]
public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags);

[DllImport("shell32.dll", SetLastError = true)]
public static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, [Out] out IntPtr pidl, uint sfgaoIn, [Out] out uint psfgaoOut);

public static void OpenFolderAndSelectItem(string folderPath, string file)
{
    IntPtr nativeFolder;
    uint psfgaoOut;
    SHParseDisplayName(folderPath, IntPtr.Zero, out nativeFolder, 0, out psfgaoOut);

    if (nativeFolder == IntPtr.Zero)
    {
        // Log error, can't find folder
        return;
    }

    IntPtr nativeFile;
    SHParseDisplayName(Path.Combine(folderPath, file), IntPtr.Zero, out nativeFile, 0, out psfgaoOut);

    IntPtr[] fileArray;
    if (nativeFile == IntPtr.Zero)
    {
        // Open the folder without the file selected if we can't find the file
        fileArray = new IntPtr[0];
    }
    else
    {
        fileArray = new IntPtr[] { nativeFile };
    }

    SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0);

    Marshal.FreeCoTaskMem(nativeFolder);
    if (nativeFile != IntPtr.Zero)
    {
        Marshal.FreeCoTaskMem(nativeFile);
    }
}
like image 30
RandomEngy Avatar answered Sep 18 '22 02:09

RandomEngy


Samuel Yang answer tripped me up, here is my 3 cents worth.

Adrian Hum is right, make sure you put quotes around your filename. Not because it can't handle spaces as zourtney pointed out, but because it will recognize the commas (and possibly other characters) in filenames as separate arguments. So it should look as Adrian Hum suggested.

string argument = "/select, \"" + filePath +"\"";
like image 24
BobWinters Avatar answered Sep 21 '22 02:09

BobWinters