我正在自动刷新图标缓存,我看到了TaskKill
和Stop-Process
之间的有趣区别。一般来说,我更喜欢使用原生PowerShell而不是从PowerShell启动的DOS命令行内容,所以我更喜欢使用Stop-Process -name:explorer
而不是Start-Process TaskKill "/f /im explorer.exe" -NoNewWindow
来停止Explorer.exe,这样DB文件就不再“使用”并且可以被删除。但是,前者允许Explorer.exe立即重新启动,因此我需要删除的图标DB文件仍在使用中,我无法删除它们。后者确实杀死了Explorer.exe,稍后我不得不使用Start-Process。有没有办法使用Stop- TaskKill获得进程行为,或者这是一种罕见的情况,老式的杂乱无章也是唯一有效的方法?
发布于 2019-10-09 01:59:05
我做了一些研究,你最好通过发送消息来结束浏览器。
对于此WM_EXITEXPLORER (1460),您可以通过消息通知资源管理器已关闭。
下面是我为Windows10编写的代码:
$code = @'
[DllImport("user32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Unicode)] public static extern IntPtr PostMessage(IntPtr hWnd, int Msg, uint wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "FindWindowW", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@
$myAPI = Add-Type -MemberDefinition $code -Name myAPI -PassThru
$myAPI::PostMessage($myAPI::FindWindow("Shell_TrayWnd", $Null),1460,0,0)
Start-Sleep -Seconds 10
最好是等待资源管理器窗口关闭,也许我会在明天添加。现在,等待10秒应该足以让explorer.exe正常结束。
这比使用kill要好得多!
发布于 2019-10-09 03:57:33
默认情况下,当被Stop-Process
停止时,Explorer.exe将自动重新启动。这是由注册表项HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
中的注册表DWORD设置AutoRestartShell
处理的。
当然,您可以使用以下命令停止该行为
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoRestartShell" -Value 0 -Type DWord
如果您使用的是不理解参数-Type
的旧PowerShell版本,则应该可以这样做:
[Microsoft.Win32.Registry]::SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon","AutoRestartShell",0,[Microsoft.Win32.RegistryValueKind]::DWord)
然后在您的代码中,停止explore进程,删除图标DB文件,然后再次启动process explorer。
最后,将注册表值重置为1
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoRestartShell" -Value 1 -Type DWord
https://stackoverflow.com/questions/58288459
复制相似问题