启动 explorer.exe 而不创建 window C#
Start explorer.exe without creating a window C#
我有一个重新启动的程序 explorer.exe
这是我杀死 explorer.exe
的代码
Process[] process = Process.GetProcessesByName("explorer.exe");
foreach (Process theprocess in process) {
theprocess.Kill();
}
以下代码成功运行并停止 explorer.exe
这是我启动 explorer.exe
的代码
Process.Start("explorer");
这也有效,但它还会创建一个 Windows 资源管理器 window 以及启动 explorer.exe
进程。
我的问题是,如何在不创建Windows 资源管理器window 的情况下启动explorer.exe
?立即关闭资源管理器window 也可以作为解决方案。
我不知道如何在不打开 window 的情况下启动资源管理器,但是您可以使用 SHDocVW.dll
中的 ShellWindows
界面来枚举资源管理器 windows as explained here 然后关闭弹出的 window:
// 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();
}
如果资源管理器不是运行,调用完整路径就可以explorer.exe:
Process.Start(
Path.Combine(
Environment.GetEnvironmentVariable("windir"), "explorer.exe"
)
);
这只会在资源管理器已经 运行 时打开一个 window,否则只会打开任务栏。
我有一个重新启动的程序 explorer.exe
这是我杀死 explorer.exe
Process[] process = Process.GetProcessesByName("explorer.exe");
foreach (Process theprocess in process) {
theprocess.Kill();
}
以下代码成功运行并停止 explorer.exe
这是我启动 explorer.exe
Process.Start("explorer");
这也有效,但它还会创建一个 Windows 资源管理器 window 以及启动 explorer.exe
进程。
我的问题是,如何在不创建Windows 资源管理器window 的情况下启动explorer.exe
?立即关闭资源管理器window 也可以作为解决方案。
我不知道如何在不打开 window 的情况下启动资源管理器,但是您可以使用 SHDocVW.dll
中的 ShellWindows
界面来枚举资源管理器 windows as explained here 然后关闭弹出的 window:
// 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();
}
如果资源管理器不是运行,调用完整路径就可以explorer.exe:
Process.Start(
Path.Combine(
Environment.GetEnvironmentVariable("windir"), "explorer.exe"
)
);
这只会在资源管理器已经 运行 时打开一个 window,否则只会打开任务栏。