刷新 Windows 资源管理器窗口(无法让内联 C# 代码在 PowerShell 函数中正常运行)

问题描述 投票:0回答:3

我正在尝试在 PowerShell 中运行一些内联 C# 代码,目的是刷新 Windows 资源管理器。

这是代码(基于这个问题):

function Request-ExplorerRefresh {

$code = @"
using System;
namespace VSYSRefresh
{
    public static class Util
    {

        public static void RefreshExplorer(){

            Console.WriteLine("Refreshing Explorer");
            
            Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
            Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);

            object shellApplication = Activator.CreateInstance(shellApplicationType);
            object windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });

            Type windowsType = windows.GetType();
            object count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
            for (int i = 0; i < (int)count; i++)
            {
                object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
                if(item != null){
                    Type itemType = item.GetType();
                }
                // only refresh windows explorer
                string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
                if ((itemName == "Windows Explorer") || (itemName == "File Explorer")) {
                    itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, null, item, null);
                }
            }
        }
    }
}
"@
    Add-Type -TypeDefinition $code -Language CSharp
    Invoke-Expression "[VSYSRefresh.Util]::RefreshExplorer()"

}

我收到以下错误:

(26,43): error CS0165: Use of unassigned local variable 'itemType'
(string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);

Cannot add type. Compilation errors occurred.

Unable to find type [VSYSRefresh.Util].

我什至无法让

Console.WriteLine()
去工作。我是否需要引用某些程序集才能使其正常工作?

我陷入了困境,任何帮助将不胜感激。

c# powershell windows-explorer
3个回答
3
投票

错误出现在您的 for 循环中:

                if(item != null){
                    Type itemType = item.GetType();
                }
                // only refresh windows explorer
                string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);

由于您在

Type itemType
语句中声明了
if
,因此下一行将无法运行,因为它脱离了上下文。将此块更改为:

                if (item != null) {
                    Type itemType = item.GetType();
                    // only refresh windows explorer
                    string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
                    if ((itemName == "Windows Explorer") || (itemName == "File Explorer")) {
                        itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, null, item, null);
                    }
                }

0
投票

补充现有答案,解决您的当前代码的问题,这里是代码仅使用PowerShell的重写。借助

New-Object
命令,可以通过指定其 ProgID:

在 PowerShell 中创建 COM 对象
'Refreshing Explorer'

$shellApplication = New-Object -ComObject Shell.Application
$windows = $shellApplication.Windows()
$count = $windows.Count()

foreach( $i in 0..($count-1) ) {
    $item = $windows.Item( $i )
    if( $item.Name() -like '*Explorer*' ) {
        $item.Refresh()
    }    
}

非常简单。唯一需要注意的是 COM 对象的属性像方法一样被调用。

此外,在我的系统上,资源管理器窗口仅命名为“Explorer”,因此我使条件更加通用。


0
投票

PowerShell 一句:

@((New-Object -com shell.application).Windows()).ForEach{$_.Refresh()}
© www.soinside.com 2019 - 2024. All rights reserved.