c# - InvokeMember 获取特定属性值的可能值

标签 c# .net windows com windows-explorer

我指的是 this thread刷新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 }); 我们将获取当前窗口对象,然后我们使用 .InvokeMember("Name".. 获取该对象的名称,同样我应该将什么传递给 InvokeMember 方法来获取该对象的路径或该窗口的标题?或者谁能告诉我上述语句中 "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);

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

提前致谢

最佳答案

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

项目 > 添加引用 > COM 选项卡。勾选“Microsoft Internet Controls”和“Microsoft Shell Controls and Automation”。现在,您可以利用 IntelliSense 的所有优势以早期绑定(bind)、美观和紧凑的方式编写它,以帮助您找到正确的成员并避免拼写错误:

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 驱动器上的任何资源管理器窗口。请注意 Path 属性对于发现显示的内容没有用,需要 LocationURL。您可能需要找出 Internet Explorer 和 Windows Explorer 窗口(又名“文件资源管理器”)之间的区别,尽管 IE 也可以显示目录内容,所以我认为这是最正确的版本。

如果你真的想做这个后期绑定(bind),那么使用 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”。

关于c# - InvokeMember 获取特定属性值的可能值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43317248/

相关文章:

c# - 单击 asp 按钮获取下拉列表的值

c# - 我的脚本只更新文本一次

c# - 尝试升级到 .NET Framework 4

.net - 在库 : How to add DLL reference to component when component is located in project hosting the library? 中进行代码内编译

c# - 如何在 asp.net-MVC2 中传递列表

c# - 使用 FTP 协议(protocol)列出包含大约 2,000,000 个文件的目录

c# - MVC 3 Ajax.ActionLink 了解一些东西

c++ - 为什么我得到错误?重载运算符 []

c++ - Windows 的 Boost Mutex 实现

windows - ssh : Could not resolve hostname github. com + STATUS_ACCESS_VIOLATION 在 eip=68086014