Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# InvokeMember获取特定属性值的可能值_C#_.net_Windows_Com_Windows Explorer - Fatal编程技术网

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

C# InvokeMember获取特定属性值的可能值,c#,.net,windows,com,windows-explorer,C#,.net,Windows,Com,Windows Explorer,我指的是刷新windows资源管理器,我只想刷新一些窗口,这意味着我想根据它们的标题或路径过滤打开的窗口。让我从该线程复制代码,以获得更多澄清: Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000"); Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true); object shellApp

我指的是刷新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);
    }
}

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


提前感谢

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

项目>添加引用>COM选项卡。勾选“Microsoft Internet控件”和“Microsoft Shell控件和自动化”。现在,您可以使用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驱动器上的资源管理器窗口。请注意,Path属性对于发现显示的内容没有什么用处,因此需要LocationURL。您可能必须找到Internet Explorer和Windows Explorer Windows(又名“文件浏览器”)之间的区别,尽管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”

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