198

I saw the other topic and I'm having another problem. The process is starting (saw at task manager) but the folder is not opening on my screen. What's wrong?

System.Diagnostics.Process.Start("explorer.exe", @"c:\teste");
Palec
  • 12,743
  • 8
  • 69
  • 138
Daniel
  • 2,868
  • 4
  • 26
  • 24
  • are you sure your path is correct? – Nathan Koop Jul 15 '09 at 16:31
  • 1
    Why do you want to call Explorer manually? Why not just open the folder, i.e. call Process.Start with a ProcessStartInfo with UseShellExecute set to true and Verb set to "open"? – OregonGhost Jul 15 '09 at 16:31
  • 1
    Yes, I tried opening 'explorer.exe' without the path and didn't work either. – Daniel Jul 15 '09 at 16:32
  • Well, I didn't post because the question is not how to open a folder, but rather how to run explorer.exe to open a folder. I just wanted to know why you want to invoke explorer directly in the first place, because there might be a reason ;) – OregonGhost Jul 15 '09 at 16:37
  • I just want to exclude options here, so this may be a stupid question: you are not doing this in Linux using mono, right? We are talking a Windows environment? – Fredrik Mörk Jul 15 '09 at 16:39
  • Yes, Visual Studio | Windows Vista Business – Daniel Jul 15 '09 at 16:40

13 Answers13

328

Have you made sure that the folder "c:\teste" exists? If it doesn't, explorer will open showing some default folder (in my case "C:\Users\[user name]\Documents").

Update

I have tried the following variations:

// opens the folder in explorer
Process.Start(@"c:\temp");
// opens the folder in explorer
Process.Start("explorer.exe", @"c:\temp");
// throws exception
Process.Start(@"c:\does_not_exist");
// opens explorer, showing some other folder)
Process.Start("explorer.exe", @"c:\does_not_exist");

If none of these (well, except the one that throws an exception) work on your computer, I don't think that the problem lies in the code, but in the environment. If that is the case, I would try one (or both) of the following:

  • Open the Run dialog, enter "explorer.exe" and hit enter
  • Open a command prompt, type "explorer.exe" and hit enter
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Well, I'm sure and if it didn't exist, would open any folder same way, or not ? – Daniel Jul 15 '09 at 16:35
  • Well, then it might be environment problem .. I opened explorer.exe through cmd and opened normal .. None of the Processs.Start worked except 'Process.Start(@"c:\does_not_exist");' that thrown an exception – Daniel Jul 15 '09 at 16:53
  • 2
    Small difference if that explorer window is already open: `Process.Start(path)` activates the window (may only blink in task bar, not brought to front); `explorer.exe`+parameter opens a new window always in the front (but multiple times the same window). So both have caveats. – KekuSemau Dec 06 '16 at 09:32
  • 1
    `Process.Start(@"c:\temp")` must be used with caution. If `c:\temp.com` exists, then the function call will open `c:\temp.com` instead. See https://forums.iis.net/p/1239773/2144186.aspx for more details. – Lex Li Nov 02 '18 at 16:15
  • 1
    Note that `Process.Start(@"c:\temp")` is susceptible to opening a different folder such as `C:\temp.exe` or `C:\temp.cmd`. See [this issue where VS itself exhibits buggy behavior](https://developercommunity.visualstudio.com/content/problem/104305/.html). You can avoid this by either using the `explorer.exe` variant or (better, IMO) always appending a `Path.DirectorySeparatorChar`. For example, `Process.Start(@"C:\temp\")`. – binki Nov 14 '18 at 16:14
  • This worked for me using .NET Framework but doesn't work using .NET Core 3. I had to create a `ProcessStartInfo` and set `UseShellExecute = true`. – Walt D Sep 29 '19 at 22:35
  • really nice. Now the question is: is tehre a way to open the explorer window to a certain position of the screen and with certain dimension? – Andrea_86 Oct 16 '21 at 11:12
84

Just for completeness, if all you want to do is to open a folder, use this:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
    FileName = "C:\\teste\\",
    UseShellExecute = true,
    Verb = "open"
});

Ensure FileName ends with Path.DirectorySeparatorChar to make it unambiguously point to a folder. (Thanks to @binki.)

This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.

idbrii
  • 10,975
  • 5
  • 66
  • 107
OregonGhost
  • 23,359
  • 7
  • 71
  • 108
  • This works for me, both on Windows and on Linux using Mono. – Menno Deij - van Rijswijk Aug 31 '16 at 08:10
  • 3
    If you use this method and a folder such as `C:\teste.exe` or `C:\teste.cmd` exists, Explorer will open to that other folder instead of the one you intended. To avoid this, you can append a `Path.DirectorySeparatorChar` to the path. See [how VS itself makes the same mistake](https://developercommunity.visualstudio.com/content/problem/104305/.html). – binki Nov 14 '18 at 16:16
  • Given @Scyssion's answer using "/select", you'd think you could use `Verb = "select"`, but alas you cannot. Regardless, great answer! – idbrii Jul 24 '19 at 22:48
  • 1
    This works for me in .NET Core 3, unlike the above accepted answer. Setting `Verb = "open"` was not necessary. (Tested in Windows, other OS's may differ.) – Walt D Sep 29 '19 at 22:36
  • You can get the applicable verbs from the `.Verbs` property on `ProcessStartInfo` (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.verb?view=netframework-4.8#remarks) – GaryNg Apr 20 '20 at 15:09
  • This works on Unity (Windows) too, if you have to compile it later and so you cannot use the built-in EditorUtilities – meyo Nov 06 '20 at 15:56
  • Compare https://stackoverflow.com/a/35032232/340790 . – JdeBP Dec 08 '21 at 15:20
26

If you want to select the file or folder you can use the following:

Process.Start("explorer.exe", "/select, c:\\teste");
Scyssion
  • 374
  • 5
  • 7
5

You're using the @ symbol, which removes the need for escaping your backslashes.

Remove the @ or replace \\ with \

Kevin Laity
  • 2,489
  • 1
  • 27
  • 34
5

You don't need the double backslash when using unescaped strings:

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
4

You should use one of the System.Diagnostics.Process.Start() overloads. It's quite simple!

If you don't place the filename of the process you want to run (explorer.exe), the system will recognize it as a valid folder path and try to attach it to the already running Explorer process. In this case, if the folder is already open, Explorer will do nothing.

If you place the filename of the process (as you did), the system will try to run a new instance of the process, passing the second string as a parameter. If the string is a valid folder, it is opened on the newly created process, if not, the new process will do nothing.

I don't know how invalid folder paths are treated by the process in any case. Using System.IO.Directory.Exists() should be enough to ensure that.

voytek
  • 2,202
  • 3
  • 28
  • 44
  • 1
    Don’t forget that you need to append a `Path.DirectorySeparatorChar`. Otherwise, if a folder with the same name but `.cmd` or `.exe` or possibly other suffixes also exists, Explorer will open to that other folder—or if those are actually executables or scripts, it will run them instead of opening the folder as you intended. – binki Nov 14 '18 at 16:18
1

You're escaping the backslash when the at sign does that for you.

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
1

Use an overloaded version of the method that takes a ProcessStartInfo instance and set the ProcessWindowStyle property to a value that works for you.

Jeremy Cron
  • 2,404
  • 3
  • 25
  • 30
1
System.Diagnostics.Process.Start("explorer.exe",@"c:\teste"); 

This code works fine from the VS2010 environment and opens the local folder properly, but if you host the same application in IIS and try to open then it will fail for sure.

mike_m
  • 1,526
  • 4
  • 14
  • 19
1

Ive just had this issue, and i found out why. my reason isnt listed here so anyone else who gets this issue and none of these fix it.

If you run Visual Studio as another user and attempt to use Process.Start it will run in that users context and you will not see it on your screen.

New Bee
  • 991
  • 1
  • 13
  • 24
0

Does it open correctly when you run "explorer.exe c:\teste" from your start menu? How long have you been trying this? I see a similar behavior when my machine has a lot of processes and when I open a new process(sets say IE)..it starts in the task manager but does not show up in the front end. Have you tried a restart?

The following code should open a new explorer instance

class sample{

static void Main()
{
  System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
}
}
schar
  • 2,618
  • 5
  • 25
  • 24
0

Do you have a lot of applications running when you are trying this? I encounter weird behavior at work sometimes because my system runs out of GDI Handles as I have so many windows open (our apps use alot).

When this happens, windows and context menus no long appear until I close something to free up some GDI handles.

The default limit in XP and Vista is 10000. It is not uncommon for my DevStudio to have 1500 GDI handles, so if you have a couple of copies of Dev studio open, it can eat them up pretty quickly. You can add a column in TaskManager to see how many handles are being used by each process.

There is a registry tweak you can do to increase the limit.

For more information see http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx

Curtis
  • 1,189
  • 2
  • 11
  • 22
0

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

Just change the path or declare it in a string

voytek
  • 2,202
  • 3
  • 28
  • 44