FastFarm
FastFarm

Reputation: 429

Start explorer.exe without creating a window C#

I have a program that restarts explorer.exe
Here is my code for killing explorer.exe

Process[] process = Process.GetProcessesByName("explorer.exe");

foreach (Process theprocess in process) {
    theprocess.Kill();
}

The following code work successfully and stops explorer.exe
Here is my code for starting explorer.exe

Process.Start("explorer");

This also works, but it also creates a Windows Explorer window as well as starting the explorer.exe process.

My question is, how can I start explorer.exe without creating a Windows Explorer window?Immediately closing the explorer window could also be considered as an answer.

Upvotes: 1

Views: 1997

Answers (3)

Alfie
Alfie

Reputation: 2350

I found a much nicer solution to this after encountering an issue where the taskbar would not always appear when starting explorer:

Process.Start(Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));

If you specify the full path like this, it will not create a window and will only load the taskbar if explorer is not already running.

Taken from this answer.

Upvotes: 0

Martin Braun
Martin Braun

Reputation: 12639

If the explorer is not running, it is enough to call the full path to explorer.exe:

Process.Start(
    Path.Combine(
        Environment.GetEnvironmentVariable("windir"), "explorer.exe"
    )
);

This will only open a window when the explorer is already running, otherwise it just opens the taskbar.

Upvotes: 2

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

I don't know how to start explorer without opening a window, but you can use the ShellWindows interface from SHDocVW.dll to enumerate explorer windows as explained here and then close the window as it pops up:

// Kill explorer
Process[] procs = Process.GetProcessesByName("explorer");
foreach (Process p in procs)
{
    p.Kill();
}

// Revive explorer
Process.Start("explorer.exe");

// Wait for explorer window to appear
ShellWindows windows;
while ((windows = new SHDocVw.ShellWindows()).Count == 0)
{
    Thread.Sleep(50);
}

foreach (InternetExplorer p in windows)
{
    // Close explorer window
    if(Path.GetFileNameWithoutExtension(p.FullName.ToLower()) == "explorer")
        p.Quit();
}

Upvotes: 2

Related Questions