25

I have problem with opening specific folder in VB.net in Windows Explorer. I used

Process.Start("explorer.exe", "Folder_Path")

Always when i tried this it open documents in explorer , whatever i wrote. Pls help.

Marko Stojkovic
  • 3,255
  • 4
  • 19
  • 20

7 Answers7

52
Process.Start("directory path")
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Marko Stojkovic
  • 3,255
  • 4
  • 19
  • 20
  • 2
    This fails on Windows 10 with "Access denied" exception, even if I wrap the path in double quotes. However, specifying "explorer.exe" and passing the path as an argument--like all the other answers--works. Despite the error, it is not about folder access rights. – C Perkins Sep 21 '21 at 16:45
8

Try opening it with:

Process.Start("explorer.exe", "/root,Folder_Path")

Or change the path before:

SetCurrentDirectory("Folder_Path")
Process.Start("explorer.exe")

And if it still fails, go with the shell command:

Shell("explorer Folder_Path", AppWinStyle.NormalFocus)
Vince
  • 3,497
  • 2
  • 19
  • 15
8

You could start explorer with preselected directory like this:

Process.Start("explorer.exe", String.Format("/n, /e, {0}", "d:\yourdirectory\"))

The Windows Explorer options are explained in this Microsoft KB article.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
  • 1
    @Chad Gilbert: The question relates to VB.NET and not C#. All strings in VB.NET are verbatim strings. The @ character in VB.NET produces an error! – keenthinker Mar 01 '18 at 20:39
  • Why bother linking KB articles since MicroShit keeps re-arranging and the link gets lost. The title was "Windows Explorer Command-Line Options" – sproketboy Dec 18 '20 at 17:59
  • @sproketboy internet links can change and go broken over time. At the time as the answer was given, 2 years ago, the link was working and IMO it was a helpful addition. Yes,, it would have been nice to redirect the KB article from msdn to the new https://learn.microsoft.com/ page. – keenthinker Dec 22 '20 at 16:24
5

The reason why it opens the default directory (MyDocuments) only could be one of these two reasons:

  • The directory does not exist.

  • The directory path contains spaces in the name, and arguments containing spaces should be enclosed with doublequotes, this is a BASIC rule of programming.

Then use the syntax properly:

    Dim Proc As String = "Explorer.exe"

    Dim Args As String =
       ControlChars.Quote &
       IO.Path.Combine("C:\", "Folder with spaces in the name") &
       ControlChars.Quote

    Process.Start(Proc, Args)
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
3
Process.Start("explorer.exe", "/select," + "C:\File_Name.txt")

The .txt could be what ever u need.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • This only works if `C:\File_Name.txt` exists. Otherwise explorer.exe opens on the root directory, especially, if the filename would be `C:\temp\subdir\File_Name.txt`. It does not open in `C:\temp\subdir`. – PeterCo Nov 26 '16 at 16:02
2

You can try Process.Start("explorer.exe", "Folder_Path") like you said.

The only reason that Windows Explorer opens the Documents folder is that you mistyped the "folder_path" and the specified folder does not exist.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Ruby Kousinovali
  • 337
  • 2
  • 20
0

I know this is an old question but there is no reason to get overly complicated; so, just use like this:

Process.Start("explorer.exe", Chr(34) & "folder to open" & Chr(34))
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35