InvokeMember 获取特定 属性 值的可能值

Possible values for InvokeMember to get specific property value

我指的是this thread刷新windows资源管理器,我只想刷新一些windows,也就是说我想过滤打开的windows他们的标题或路径。让我从该线程复制代码以获得更多说明:

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 });
    Type itemType = item.GetType();

    string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
    if (itemName == "Windows Explorer")
    {
        // Here I want to check whether this window need to be refreshed
        // based on the opened path in that window
        // or with the title of that window
        // How do I check that here
        itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, null, item, null);
    }
}

我从上面的代码中了解到的是:通过使用这一行 windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i }); 我们将得到当前的 window object,然后我们使用 .InvokeMember("Name".. 来获取 object 的名称,同样我应该将什么传递给 InvokeMember 方法来获取 object 的路径或 window 的标题?或者谁能​​告诉我上述语句中 "Name" 的可能替代值?

我期待的是如下代码:

 string itemPath = (string)itemType.InvokeMember("Something here", System.Reflection.BindingFlags.GetProperty, null, item, null);

 string itemTitle = (string)itemType.InvokeMember("Something here", System.Reflection.BindingFlags.GetProperty, null, item, null);

如果你需要我可以给你更多的信息,期待专家的建议来解决这个问题,

提前致谢

这就是您在糟糕的旧时代编写后期绑定 COM 客户端代码的方式。相当大的痛苦和痛苦让它继续下去,片段中的内容还没有结束。我将首先提出一种非常不同的方法来执行此操作,因为这些 COM 对象在任何 Windows 版本上都可用并且永远不会再更改,所以后期绑定没有任何意义。自 VS2010 以来支持的 "Embed Interop Types" 功能消除了避免它的任何充分理由。

项目 > 添加引用 > COM 选项卡。勾选 "Microsoft Internet Controls" 和 "Microsoft Shell Controls and Automation"。现在您可以尽早编写它,利用 IntelliSense 的所有好处来帮助您找到正确的成员并避免拼写错误:

var shl = new Shell32.Shell();
foreach (SHDocVw.InternetExplorer win in shl.Windows()) {
    var path = win.LocationURL;
    if (!path.StartsWith("file:///")) continue;
    path = System.IO.Path.GetFullPath(path.Substring(8));
    if (path.StartsWith("C")) win.Refresh();
}

一个有点傻的例子,它会刷新显示路径位于 C 盘的任何资源管理器 window。请注意 Path 属性 如何对发现显示的内容没有用,需要 LocationURL。您可能需要找到 Internet Explorer 和 Windows Explorer windows(又名 "File Explorer")之间的区别,尽管 IE 也可以显示目录内容,所以我认为这是最正确的版本。

如果你真的想做这个后期绑定然后使用 dynamic 关键字来减少痛苦。在这种情况下几乎相同:

dynamic shl = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
foreach (var win in shl.Windows()) {
    string path = win.LocationURL;
    if (!path.StartsWith("file:///")) continue;
    path = System.IO.Path.GetFullPath(path.Substring(8));
    if (path.StartsWith("C")) win.Refresh();
}

明确回答您的问题,使用"LocationURL"。