[c#] Open a folder using Process.Start

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

This question is related to c# explorer

The answer is


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.


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


Strange.

If it can't find explorer.exe, you should get an exception. If it can't find the folder, it should still open some folder (eg my Documents)

You say another copy of Explorer appears in the taskmanager, but you can't see it.

Is it possible that it is opening offscreen (ie another monitor)?

Or are you by any chance doing this in a non-interactive service?


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.


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

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

Remove the @ or replace \\ with \


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.


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

Just change the path or declare it in a string


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.


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


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

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

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

Process.Start("explorer.exe", "/select, c:\\teste");

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

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