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

以下是代码(基于this question):

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()美元go 工作.我是否需要引用某些程序集才能使其正常工作?

我走进了死胡同,任何帮助都将不胜感激.

推荐答案

错误在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);

由于您在if语句中声明了Type itemType,因此下一行将无法运行,因为它断章取义.将此块更改为:

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

Csharp相关问答推荐

react 式扩展连接中的非交叉LeftDurationTimeout

. NET 8 HttpClient post参数将其情况更改为camel'

哪个nuget包含SecurityStampValidatorOptions

. net依赖注入如何避免服务类中的新

Blazor-从数据库内部服务器提取错误

.NET 6控制台应用程序,RabbitMQ消费不工作时,它的程序文件中的S

C#XmlSerializer-输出控制新行的多个XML片段

Rx.Net窗口内部可观测数据提前完成

WinForms在Linux上的JetBrains Rider中的应用

Automapper 12.x将GUID映射到字符串

有空容错运算符的对立面吗?

获取混淆&Quot;模糊引用&Quot;错误

C#中类库项目的源代码生成器

使用可空引用类型时C#接口实现错误

C#System.Commandline:如何向命令添加参数以便向其传递值?

C#USB条形码 scanner 在第二次扫描时未写入行尾字符

将ValueTask发送给调用者

初始化具有EntityFrameworkCore和不同架构的数据库时引发System.MissingMethodExcept

使用c#中的Windows 10关机消息

从route获取URL参数,无需指定@page route - Blazor