Reputation: 1821
When I start an explorer window with a directory path, a new explorer.exe process is spawned. After my program exits the explorer.exe process stays open which is ok, but when I close that explorer window the process doesn't close. If I spawned several explorer windows in my program, I end up with many explorer.exe process that stay around even when all the explorer windows are closed.
ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe", folderPath);
Process.Start(startInfo);
However, if I were to start just the explorer.exe with no 2nd argument, an extra explorer.exe process will not be created even though an explorer window is opened. When that explore window is closed, you also don't see any extra explorer.exe process other than the desktop process one. Please note that my desktop explore.exe is PID 7704 is this example and that should be the only one running.
ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe", folderPath);
Process.Start(startInfo);
I found that this is true if I manually do this launching using cmd.exe. In the command prompt, if you run "explorer.exe C:/", you can create this same "dangling" explorer.exe process while running just "explorer.exe" doesn't.
So my question is how do I open an explorer window for the folder I want without creating all these extra explorer.exe processes. I also wonder whether the "dangling" explorer.exe processes would eventually go away on their own or they would sit there and hog all the memory until I have to force end task. So far I have seen them persisted for days and never going away even though there are no explorer windows associated with them anymore after the windows are closed.
Only thing I can think of so far is to open an explorer.exe with no arguments so it doesn't create the extra process then somehow figure out a way to find that open explorer window handle then use it to browse to the directory I want. I have seen some DLLImport("user32.dll") that might be able to do this.
It sounds much more difficult than just using the Process.Start to get to the folder I want right away. I just don't think it's a good idea for my program to leave a lot of memory drain processes behind even though it's something you can already do with cmd.exe. I don't know whether this is a Windows "feature" that eventually ends in an out of memory situation.
Upvotes: 0
Views: 2309
Reputation: 17
I just happened to have done something like this just now and maybe helped
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Process::Start("explorer.exe", "/open, C:\\Users\\mywindows\\source\\repos\\Kuliah\\Debug\\Praktikum Pertemuan 2.exe");
}
dont forget to add library
using namespace System::Diagnostics;
just try simple code
Process::Start("explorer.exe");
Upvotes: -1
Reputation: 7187
Here is a solution which creates no additional explorer.exe processes, and uses the existing one as it seems.
I realized this when opening the folder from a command shell. When run "explorer c:\temp", a new explorer.exe is created. But when run "start c:\temp"
..
So the following code starts a hidden cmd process, feeds its input with "start <folder>\r\n"
, and "exit\r\n"
afterward to close it. The code also checks if the new cmd process is leaked.
Hope this helps.
Process cmd = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true
}
};
cmd.Start();
int processId = cmd.Id;
Debug.WriteLine("PID: {0}", processId);
cmd.StandardInput.Write("start " + folderPath + "\r\n");
cmd.StandardInput.Write("exit\r\n");
cmd = null;
try
{
Process isCmdStillThere = Process.GetProcessById(processId);
}
catch (Exception errorQueryingProcess)
{
Debug.Assert(errorQueryingProcess.Message == "Process with an Id of " + processId + " is not running.");
}
Upvotes: 1