windows 重新启动 explorer.exe 只会打开一个资源管理器窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10954041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:34:44  来源:igfitidea点击:

Restarting explorer.exe only opens an explorer window

windowsbatch-fileexplorertaskkill

提问by mythofechelon

The Problem

问题

In one part of a batch file (kind of, see Extra Information) I need to restart Explorer, so I use the, tried-and-tested method of

在批处理文件的一部分(某种,请参阅额外信息)中,我需要重新启动资源管理器,因此我使用经过验证的方法

taskkill /f /im explorer.exe >nul
explorer.exe

Then this happens

然后发生这种情况

  1. explorer.exeis successfully terminated
  2. explorer.exeis started (see Image 2), but only an Explorer window opens, which I am left with indefinitely (see Image 1)
  1. explorer.exe已成功终止
  2. explorer.exe已启动(参见图 2),但只打开了一个资源管理器窗口,我无限期地保留了该窗口(参见图 1)

I can then only properly restart Explorer by starting a new task from Task Manager, as, I'm assuming, Win + Ris part of Explorer.

然后我只能通过从任务管理器启动新任务来正确重新启动资源管理器,因为我假设它Win + R是资源管理器的一部分。

Extra Information

额外的信息

Now, I say "kind of" as I'm running the batch file from a self-executing SFX archive, created with WinRAR. So, when executed, the contents of the archive are extracted to %temp%and a user-defined file (usually a boot-strapper and, in this case, my batch file) is run upon successful extraction.

现在,我说“有点”,因为我正在从使用 WinRAR 创建的自执行 SFX 存档中运行批处理文件。因此,当执行时,存档的内容被提取到%temp%用户定义的文件(通常是引导程序,在这种情况下,我的批处理文件)在成功提取后运行。

So far, I've deduced

到目前为止,我已经推断

  1. explorer.exedefinitely is being fully killed.
  2. The batch file definitely is called and executed correctly, as it runs and everything else in the script works as designed, except for the line that starts explorer.exe
  3. The command to restart Explorer isn't "badly timed", or anything, as I've tried delaying it.
  4. The batch file works perfectly when manually extracted from the archive, so it's not a problem with the compression or extraction processes.
  5. Even with commands like start explorer.exe | cmd.exeExplorer doesn't restart properly, so it's definitely not a problem with the .batfile.
  1. explorer.exe绝对是被彻底杀死。
  2. 批处理文件肯定会被正确调用和执行,因为它运行并且脚本中的所有其他内容都按设计工作,除了开始的行 explorer.exe
  3. 重新启动资源管理器的命令不是“时间不好”或任何东西,因为我已经尝试推迟它。
  4. 当从存档中手动提取时,批处理文件可以完美运行,因此压缩或提取过程没有问题。
  5. 即使使用start explorer.exe | cmd.exe资源管理器之类的命令也无法正常重新启动,因此.bat文件绝对不是问题。

I can confirm that it works on Windows XP and Windows 7 x86 but not Windows 7 x64 (which is my system).

我可以确认它适用于 Windows XP 和 Windows 7 x86,但不适用于 Windows 7 x64(这是我的系统)。

Status

地位

At the moment, I'm suspicious of WinRAR, as I've proved that the code itself works. So, I'm creating the self-executing SFX with different versions of WinRAR. So far, I've tried versions:

目前,我对 WinRAR 持怀疑态度,因为我已经证明代码本身有效。因此,我正在使用不同版本的 WinRAR 创建自执行 SFX。到目前为止,我已经尝试过以下版本:

  • 4.11 x86
  • 4.11 x64
  • 4.20b3 x86
  • 4.20b3 x64
  • 4.11 x86
  • 4.11 x64
  • 4.20b3 x86
  • 4.20b3 x64

and had the same results every time.

并且每次都有相同的结果。

I submitted a bug report to [email protected] yesterday and got a reply from Eugene Roshal himselfthis morning

我昨天向 [email protected] 提交了一个错误报告,今天早上收到了 Eugene Roshal 本人的回复

Hello, SFX module uses ShellExecuteEx to start a setup application. Normally it works well. I do not know why Explorer decides to switch to windowed mode. Now I built a small standalone program

您好,SFX 模块使用 ShellExecuteEx 来启动安装应用程序。通常它运作良好。我不知道为什么 Explorer 决定切换到窗口模式。现在我构建了一个小的独立程序

#include <windows.h>    
void main()
{
  SHELLEXECUTEINFO si;
  memset(&si,0,sizeof(si));
  si.cbSize=sizeof(si);
  si.lpFile="test.bat";
  si.nShow=SW_SHOWNORMAL;
  ShellExecuteEx(&si);
}

which runs test.bat with contents as in your sample. This program shows exactly the same behavior as WinRAR SFX, so Explorer is started in window.

它运行 test.bat ,内容与您的示例中的内容相同。该程序显示的行为与 WinRAR SFX 完全相同,因此资源管理器在窗口中启动。

and a second email this morning

和今天早上的第二封电子邮件

Sorry, no advice now. I replaced ShellExecuteEx with CreateProcess

抱歉,现在没有建议。我用 CreateProcess 替换了 ShellExecuteEx

#include <windows.h>
void main()
{
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  memset(&si,0,sizeof(si));
  si.cb=sizeof(si);
  CreateProcess(NULL,"test.bat",NULL,NULL,TRUE,0,NULL,NULL,&si,&pi);
}

but result is the same. I tried to use other SW_ flags like SW_SHOWDEFAULT or SW_RESTORE with ShellExecuteEx also as "open" and "explore" lpVerb, but it does not help. For now I do not understand the logic behind this windowed versus desktop mode.

但结果是一样的。我尝试将其他 SW_ 标志(如 SW_SHOWDEFAULT 或 SW_RESTORE)与 ShellExecuteEx 一起用作“打开”和“探索”lpVerb,但这无济于事。现在我不明白这种窗口模式与桌面模式背后的逻辑。

I realise the outlook is grim but, I hope that's of help to someone..

我意识到前景很严峻,但我希望这对某人有所帮助。

Proof / Evidence

证明/证据

Link to an SFX archive demonstrating this, if anyone wants it: https://dl.dropbox.com/u/27573003/Social%20Distribution/restart-explorer.exe

链接到演示此的 SFX 存档,如果有人想要的话:https: //dl.dropbox.com/u/27573003/Social%20Distribution/restart-explorer.exe

image 1

图 1

image 2

图 2

You may notice here that I'm running the commands inside a VM (as denoted by VMwareTray.exe) but it is not a VM-caused conflict. I've tested the exact same files on my own host system (which is the same OS) and have had the same results.

您可能会注意到,我在 VM 中运行命令(由 表示 VMwareTray.exe),但这不是 VM 引起的冲突。我已经在我自己的主机系统(相同的操作系统)上测试了完全相同的文件,并得到了相同的结果。

Update

更新

I'm experiencing similar "works outside of an SFX archive but not from one" problems when using REG ADDin a completely different project. I just don't think SFX archives play nice with batch files.

REG ADD在完全不同的项目中使用时,我遇到了类似的“在 SFX 存档之外工作,但不是来自一个”问题。我只是认为 SFX 档案不适用于批处理文件。

采纳答案by mythofechelon

The other day, I was having a look through some of WinRAR's more advanced options and came across this tab:

前几天,我浏览了 WinRAR 的一些更高级的选项,发现了这个选项卡:

enter image description here

在此处输入图片说明

As soon as I saw that I suspected it to be part of the problem and solution, as this issue only ever occurs on Windows 7 x64.

我一看到我就怀疑它是问题和解决方案的一部分,因为这个问题只发生在 Windows 7 x64 上。

As suspected, using the Default64.SFXmodule instead of the default Default.SFXmodule entirely fixed the issue. Finally.

正如所怀疑的那样,使用Default64.SFX模块而不是默认Default.SFX模块完全解决了这个问题。最后。

回答by Doug Benham

I think user1631170 is on to something, "I wonder if some part of Win-RAR is running in 32-bit mode? Could you even start explorer64 running from a 32-bit process? I am pretty certain that Windows won't do that."

我认为 user1631170 正在做某事,“我想知道 Win-RAR 的某些部分是否在 32 位模式下运行?您甚至可以从 32 位进程启动 explorer64 吗?我很确定 Windows 不会这样做.”

When I start explorer.exe from ProcessHacker (32-bit process manager), I get an explorer window.

当我从 ProcessHacker(32 位进程管理器)启动 explorer.exe 时,我会看到一个资源管理器窗口。

But I can force it to start the 64-bit explorer with this:

但我可以强制它启动 64 位资源管理器:

%systemroot%\sysnative\cmd.exe /c start /B explorer.exe

sysnative is a keyword that Windows recognizes to bypass the file system redirection for 32-bit/64-bit (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspxEnjoy!

sysnative 是 Windows 识别为绕过 32 位/64 位文件系统重定向的关键字 ( http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85) .aspx享受!

回答by sparrowt

I had this same problem and found that all the solutions here still didn't work from a batch script.

我遇到了同样的问题,发现这里的所有解决方案仍然无法通过批处理脚本工作。

None of these worked completely:

这些都没有完全奏效:

start explorer.exe
start explorer
explorer.exe
explorer

because they all either opened a window (and didn't show the taskbar again), or the batch script then hung thereafter and couldn't execute any more commands

因为他们要么打开了一个窗口(并且没有再次显示任务栏),要么批处理脚本然后挂起并且无法执行更多命令

I found that this line in the batch file did work (after killing explorer.exe):

我发现批处理文件中的这一行确实有效(在杀死 explorer.exe 之后):

start "" "%windir%\explorer.exe"

and also allowed other commands to be executed after it in the script

并且还允许在脚本中在它之后执行其他命令

回答by Jacob Seleznev

This works in Windows 7:

这适用于 Windows 7:

taskkill /f /IM explorer.exe
start explorer.exe
exit

回答by Matt

For restarting explorer.exe, this worked for me.

为了重新启动 explorer.exe,这对我有用。

powershell.exe Stop-Process -processname explorer

回答by Arnoud Mulder

When you run explorer.exe from an 32-bit application in 64-bit windows, the path will be redirected to the SysWOW64 directory which contains the 32-bit explorer.exe.

当您在 64 位窗口中从 32 位应用程序运行 explorer.exe 时,路径将被重定向到包含 32 位 explorer.exe 的 SysWOW64 目录。

In XP64 it wasn't not such a big deal. In the taskmanager you can see the 32-bit explorer.exe running but it did start as the shell. In Windows 10 (as I came to this problem, it looks like it is introduced in Windows 7), the 32-bit explorer.exe is a stub which creates a new instance of the 64-bit explorer.exe. It probably passes a path on the commandline here so the 64-bit explorer.exe opens a window instead of starting the shell.

在 XP64 中,这不是什么大问题。在任务管理器中,您可以看到 32 位 explorer.exe 正在运行,但它确实作为 shell 启动。在 Windows 10 中(当我遇到这个问题时,它看起来像是在 Windows 7 中引入的),32 位 explorer.exe 是一个存根,它创建 64 位 explorer.exe 的新实例。它可能会在此处传递命令行上的路径,因此 64 位 explorer.exe 打开一个窗口而不是启动 shell。

So it is still like before that you can control whether a window or a shell should be started by starting explorer.exe with or without a path as commandline parameter.

因此,它仍然像以前一样,您可以通过使用或不使用路径作为命令行参数来启动 explorer.exe 来控制是否应该启动窗口或外壳程序。

Instead, you should force starting the 64-bit explorer.exe from the 32-bit application and all is ok. To do this, one method is using the sysnative directory as mentioned above. But another method is to use Wow64DisableWow64FsRedirection/Wow64RevertWow64FsRedirection.

相反,您应该从 32 位应用程序强制启动 64 位 explorer.exe,一切正常。为此,一种方法是使用上面提到的 sysnative 目录。但另一种方法是使用 Wow64DisableWow64FsRedirection/Wow64RevertWow64FsRedirection。

I did the latter and can confirm it works nicely. For both CreateProcess and ShellExecuteEx API.

我做了后者,可以确认它工作得很好。对于 CreateProcess 和 ShellExecuteEx API。

回答by panda-34

Try

尝试

%windir%\explorer.exe
start %windir%\explorer.exe
start /d%windir% explorer.exe

回答by Bali C

I have seen similar problems before doing this in C#. The process had to be invoked by calling explorer shell rather than explorer window, but I haven't had any problems in batch.

在 C# 中执行此操作之前,我见过类似的问题。该过程必须通过调用资源管理器外壳而不是资源管理器窗口来调用,但我在批处理中没有遇到任何问题。

Try using this:

尝试使用这个:

taskkill /im explorer.exe /f
explorer

The difference between the other answers being explorerrather than explorer.exewhich has caused problems before for me.

其他答案之间的区别是,explorer而不是explorer.exe之前给我造成了问题的答案。

This works on my Win7 x64 PC.

这适用于我的 Win7 x64 PC。

Hope this helps!

希望这可以帮助!

回答by Pawe? Liszka

Have same issue with Visual Studio.

Visual Studio 有同样的问题。

What works for me (Win 7 Pro 64bit):

什么对我有用(Win 7 Pro 64 位):

PPM on Project name select "Properties"

项目名称上的 PPM 选择“属性”

Configuration Properties > Build Events > Pre-Build Event

配置属性 > 构建事件 > 预构建事件

taskkill /im explorer.exe /f

taskkill /im explorer.exe /f

Configuration Properties > Build Events > Post-Build Event

配置属性 > 构建事件 > 构建后事件

start "" "C:\Windows\explorer.exe"

start "" "C:\Windows\explorer.exe"

But this make other problem (the IDE is frozen after the explorer runs) and now I'm only able to restart the IDE to run build command again...

但这会导致其他问题(在资源管理器运行后 IDE 被冻结),现在我只能重新启动 IDE 以再次运行构建命令......

回答by Pawe? Liszka

Use this (.bat with administrative privileges) in x64 or x86

在 x64 或 x86 中使用此(具有管理权限的 .bat)

tasklist /fi "imagename eq explorer*" | find /i "explorer*"
if not errorlevel 1 (taskkill /f /im "explorer*") else (
start %windir%\explorer.exe