2

I have an app that writes a series of text files to a folder on the user's machine. It then prompts them if they would like to open the folder to view all the files. I use System.Diagnostics.Process.Start() to do this and it works well. However, if there is already a window with that folder open, I would just like to reuse that window instead of opening another one. Helps keep things tidy.

Any ideas?

Update: I tried this:

 if (MessageBox.Show("Compiled! Open folder?", "Compile Done", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { ProcessStartInfo Info = new ProcessStartInfo("explorer", Path); Info.UseShellExecute = false; Process.Start(Info); } 

But it still opens a new window. I also tried setting UseShellExecute to true and I get the same result of a new window opening up.

2 Answers 2

3

Just tried this on Windows XP:

System.Diagnostics.Process.Start(@"C:\Dev"); System.Threading.Thread.Sleep(10000); System.Diagnostics.Process.Start(@"C:\Dev"); 

And it opened my C:\Dev folder. I then focused elsewhere. After 10 seconds, the C:\Dev window flashed and was highlighted again. So what are you doing different where it isn't working?

(Trying to find out if there's an open window displaying a particular folder is fraught and error prone. I wouldn't recommend it)

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, this worked! I guess windows automatically associates raw folder paths with Explorer and this somehow just happens to allow it work how I want. I guess explicitly calling explorer.exe and passing the folder path as a parameter forces a new Explorer instance every time. Thanks, mate!
Also, even if I have other Explorer windows open to different folders, this code reuses only the folder that points to the path I am trying to open. Other windows remain unaffected.
3

If you use ShellExecute() to open the folder then Explorer will do the work for you and re-use an open Explorer window if one exists.

The .net way to call ShellExecute() is to set the UseShellExecute property of the ProcessStartInfo object that is passed to Process.Start().


Update

Having now posted your code I can see the problem. You should ask the system to open the folder rather than trying to start a new explorer.exe process, as so:

ProcessStartInfo Info = new ProcessStartInfo(Path); Process.Start(Info); 

7 Comments

The Process class uses ShellExecuteEx() by default.
@hans thanks. Can't imagine why it doesn't behave as expected for OP.
I put the code I tried in my original post. What am I doing wrong? Also, you say that windows will just reuse an existing Explorer window. Not just any Explorer window, right? If I have other Explorer windows open, I don't want them to be used. I only want the Explorer window that's currently open to the folder I am trying to open to be reused.
Disregard. The other answerer here provided the solution that works as I need. I don't understand exactly how it works, but I stated my theory in his answer. Thanks for trying, though.
@oscilatingcretin My answer is fine too. The key is that you were previously opening explorer.exe when in fact the correct thing to do is to open the folder.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.