Programmatically Selecting File in Explorer

Programmatically selecting file in explorer

Found the answer to my question. I need to use the shell function SHOpenFolderAndSelectItems. Here is the code for the function if anybody is ever interested:

void BrowseToFile(LPCTSTR filename)
{
ITEMIDLIST *pidl = ILCreateFromPath(filename);
if(pidl) {
SHOpenFolderAndSelectItems(pidl,0,0,0);
ILFree(pidl);
}
}

Programmatically select multiple files in windows explorer

This should be possible with the shell function SHOpenFolderAndSelectItems

EDIT

Here is some sample code showing how to use the function in C/C++, without error checking:

//Directory to open
ITEMIDLIST *dir = ILCreateFromPath(_T("C:\\"));

//Items in directory to select
ITEMIDLIST *item1 = ILCreateFromPath(_T("C:\\Program Files\\"));
ITEMIDLIST *item2 = ILCreateFromPath(_T("C:\\Windows\\"));
const ITEMIDLIST* selection[] = {item1,item2};
UINT count = sizeof(selection) / sizeof(ITEMIDLIST);

//Perform selection
SHOpenFolderAndSelectItems(dir, count, selection, 0);

//Free resources
ILFree(dir);
ILFree(item1);
ILFree(item2);

How to open Explorer with a specific file selected?

Easiest way without using Win32 shell functions is to simply launch explorer.exe with the /select parameter. For example, launching the process

explorer.exe /select,"C:\Folder\subfolder\file.txt"

will open a new explorer window to C:\Folder\subfolder with file.txt selected.

If you wish to do it programmatically without launching a new process, you'll need to use the shell function SHOpenFolderAndSelectItems, which is what the /select command to explorer.exe will use internally. Note that this requires the use of PIDLs, and can be a real PITA if you are not familiar with how the shell APIs work.

Here's a complete, programmatic implementation of the /select approach, with path cleanup thanks to suggestions from @Bhushan and @tehDorf:

public bool ExploreFile(string filePath) {
if (!System.IO.File.Exists(filePath)) {
return false;
}
//Clean up file path so it can be navigated OK
filePath = System.IO.Path.GetFullPath(filePath);
System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
return true;
}

Reference: Explorer.exe Command-line switches

How do I display Explorer with a file selected?

You need SHOpenFolderAndSelectItems. This question was early discussed here -
Programmatically selecting file in explorer

Dont forget to call CoInitialize before first use of SHOpenFolderAndSelectItems

Opening a folder in explorer and selecting a file

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")

How to select a file from windows file explorer

Use IFileDialog, IFileOpenDialog or IFileSaveDialog

Opening an explorer window with designated file selected

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,

Open Windows Explorer and perform a search

I got it working. It was in fact very simple. Assume I am searching for files with filename containing the term "mat" in folder "C:\temp1". The explorer window has the title "CustomSearch". It works in Windows 10.

string searchLink = "search-ms:displayname=CustomSearch&crumb=filename%3A*mat%20&crumb=location:C%3A%5Ctemp1";
Process.Start(searchLink);


Related Topics



Leave a reply



Submit