tags:

views:

919

answers:

10
+6  Q: 

c# open folder

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 ..

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

What's wrong ?

Thanks,

Edited: Replaced '//' for '/', still starting only the process @ task manager, folder is not opening ..

+3  A: 

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

Remove the @ or replace \\ with \

Kevin Laity
Still not opening the folder .. Only starting the process @ task manager
Daniel
I have no further suggestions, I was able to test and make it work in Visual C# express 2008
Kevin Laity
I would say... zebras. There is something wrong with your explorer, maybe virii or someting...
Martinho Fernandes
A: 

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

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

Because you are using the @, the test inside the quotes doesn't need to be escaped. Try

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

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

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
Austin Salonen
Martinho Fernandes
here it is: http://catb.org/jargon/html/A/ASCII.html
Martinho Fernandes
ugh, I'm an idiot...
Austin Salonen
+1  A: 

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
+8  A: 

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
Thats what I was thinking. +1
shahkalpesh
Well, I'm sure and if it didn't exist, would open any folder same way, or not ?
Daniel
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
Wow. That's weird.
Fredrik Mörk
good answer
Nathan Koop
A: 

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?

sgmoore
I have only 1 monitor, and "You say another copy of Explorer appears in the taskmanager, but you can't see it." this is right .. I don't know what you meant "Or are you by any chance doing this in a non-interactive service?"
Daniel
I meant if the program you are writing is a service (which by default runs completely in the background) as opposed to a normal winforms program. (If you don't know what a service is, it is unlikely you are writing one).Going back to taskmanager, if you select either 'switch to', 'bring to front' or 'maximise' on this hidden explorer window, does it appear?
sgmoore
+1  A: 

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

I didn't bother to use a verbatim string because this §%"§%& MarkDown editor doesn't let me type an at symbol (German keyboard layout) as it interprets an Alt Gr keypress as an Alt keypress.

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

OregonGhost
Still didn't work .. :(
Daniel
A: 

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
A: 

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