【问题标题】:Start explorer.exe without creating a window C#在不创建窗口的情况下启动 explorer.exe C#
【发布时间】:2016-01-15 00:50:22
【问题描述】:

我有一个重启的程序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 资源管理器窗口并启动 explorer.exe 进程。

我的问题是,我如何在不创建 Windows 资源管理器窗口的情况下启动 explorer.exe?立即关闭资源管理器窗口也可以视为一个答案。

【问题讨论】:

标签: c# windows process


【解决方案1】:

我不知道如何在不打开窗口的情况下启动资源管理器,但是您可以使用SHDocVW.dll 中的ShellWindows 接口枚举资源管理器窗口as explained here,然后在弹出窗口时关闭它:

// 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();
}

【讨论】:

    【解决方案2】:

    如果资源管理器没有运行,调用explorer.exe的完整路径即可:

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

    这只会在资源管理器已经运行时打开一个窗口,否则它只会打开任务栏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2019-01-27
      • 2013-07-21
      相关资源
      最近更新 更多