Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple program which displays an image, and an option to "View file in folder".

Currently, I can view the folder of the image file, but would like to have the file selected in explorer where explorer launches.

For example, if the image location is "C:\Images\Test.png" I want explorer to open the directory "C:\Images" and the file Test.png to be automatically selected and in view.

What I have tried:

As of now, I launch the load the folder of the file using

Process.Start("explorer.exe", filepath.substring(0,lastindexof("\"))))
Posted
Updated 10-Mar-22 3:27am
v3
Comments
Richard MacCutchan 10-Mar-22 8:55am    
"I want to launch the directory"
What exactly does that mean? You can get the contents of a directory, or a specific file name from the Directory and File classes.
Member 15435283 10-Mar-22 9:20am    
Launch the directory of the file, and highlight file. I've updated question for clarity.
Richard MacCutchan 10-Mar-22 9:41am    
See Solution 1 below.
OriginalGriff 10-Mar-22 9:00am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Member 15435283 10-Mar-22 9:20am    
Updated question for clarity

1 solution

If i understand you well, you want to open folder in windows explorer and select specific file.

Try this:
VB.NET
Dim sFileName As String = "C:\Images\Test.png"
Process.Start("explorer.exe", "/select," & sFileName)


As is shown here[^] there's few arguments you can pass to the explorer:


/e  
/idlist,:handle:process specifies object as ITEMIDLIST in shared memory block with given
handle in context of given process
/n redundant in Windows Vista
/root,/idlist,:handle:process


/root,clsid

/root,clsid,path

/root,path
specifies object as root
/select show object as selected item in parent folder
/separate show in separate EXPLORER process
path specifies object;

ignored if object already specified;

overridden by specification in later /idlist or /root argument
 
Share this answer
 
v3
Comments
Member 15435283 10-Mar-22 9:52am    
Perfect!

Thanks, much easier than i expected. And thanks for arguments, i'll bookmark this because i'll need those soon.
Maciej Los 10-Mar-22 13:19pm    
You're very welcome. :)
Leonardo Mattera 6-Nov-23 16:23pm    
Thank you very much. It was very helpful.
Maciej Los 6-Nov-23 17:31pm    
You're very welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900