Click here to Skip to main content
15,908,841 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!

I have program which launches explorer with provided folderpath.

My code in that event:

.
.
string argument = "select," + e.Uri.LocalPath;
Process.Start("explorer.exe", argument);
.
.


Works just fine, but when selecting another path this will lauch new explorer with selected path. In the end I have multiple explorers and that is not just right...

How do I use existing application if it is already running? Im using C# and .Net4 platform.

Hope you got idea

Cheers!
Posted

Spawning of an external process is almost never a good solution; should be used as as last resort.

I don't really understand why starting the Explorer; it probably is not communicating with your application. If the user need an Explorer window, she/he can do it by herself/himself. Most users can be only irritated if you do it for them.

Consider the alternative: use System.Windows.Forms.WebBrowser control instead, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^]
.

Use a component like this:
Filesystem TreeView[^],
File Explorer using Treeview controller in C# 2.0[^],
My Explorer In C#[^].

—SA
 
Share this answer
 
v5
Comments
Espen Harlinn 26-Apr-11 15:28pm    
That should work, 5ed!
Sergey Alexandrovich Kryukov 26-Apr-11 16:38pm    
Thank you, Espen.
--SA
Wonde Tadesse 26-Apr-11 21:12pm    
@SAKryukov, @raistu want to open "explorer". Not "Internet explorer". How you suggest to use System.Windows.Forms.WebBrowser control ? Hum...
Sergey Alexandrovich Kryukov 26-Apr-11 21:47pm    
Oops! Thank you, there can be proposal for Explorer as well; I'll fix it.
Thank you very much for correcting me!
--SA
Sergey Alexandrovich Kryukov 26-Apr-11 22:04pm    
OK, content replaced :-)
--SA
Take a look at IRunningObjectTable[^]

Allows you to gain access to, and control, a running instance of IE using COM.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 26-Apr-11 15:34pm    
Good suggestion, voted 5!
Espen Harlinn 26-Apr-11 15:47pm    
Thank you, Nishant :)
Hi,
you can kill the previous process that your application started, and start a new one. In order to be sure to kill the right process, you'll need to store the BaseAddress of started process, and then compare later to the one that you want to close.
List<int32> list = new List<int32>(); //list which will have identifiers of processes that you've started
...
...
//starting a new process
Process[] procs = Process.GetProcessesByName("explorer"); //get all explorers
foreach (Process p in procs)
{
    int baseAdd = p.MainModule.BaseAddress.ToInt32();
    if(list.Contains(baseAdd))
       p.Kill(); //kill previous process
}
var r = Process.Start("explorer.exe", argument);
list.Add(r.MainModule.BaseAddress.ToInt32); //store process started by you
</int32></int32>

Regards
 
Share this answer
 
v4
You can keep track of what is already open and what not by storing them in a dictionary within your application. Thus adding some minor logic could help you.
 
Share this answer
 
You can handle Process.Exited event.

Process m = new Process();
m.EnableRaisingEvents = true;
m.Exited += new EventHandler(this.MProcess_Exited);
m.StartInfo.FileName = "explorer.exe";
m.StartInfo.Arguments = "select," + e.Uri.LocalPath;
m.Start();


// When process stops, MProcess_Exited is called.
void MProcess_Exited(object sender, EventArgs e)
{
 // Do logic ?
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900