2017-04-10 1 views
1

Windows 탐색기를 새로 고치려면 this thread을 참조하고 있습니다. 일부 창만 새로 고침합니다. 즉, 제목이나 경로에 따라 열린 창을 필터링하려고합니다. 이 라인 windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });를 사용하여 우리는 현재 창 개체를 얻을 것이다, 그리고 우리가 얻을 .InvokeMember("Name"..를 사용 : 나는 위의 코드를 이해 무엇특정 속성 값을 가져올 수있는 InvokeMember의 가능한 값

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

입니다 : 좀 더 명확한 설명을 위해 해당 스레드에서 코드를 복사하자 해당 객체의 이름, 현명한 것처럼 내가 그 객체의 경로 또는 해당 창의 제목을 얻으려면 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); 

당신이 필요하면이 문제를 해결하기 위해 전문가의 제안을 기대하고, 더 많은 정보를 제공 할 수 있습니다,

:

내가 기대하고있어 다음과 같은 몇 가지 코드는

미리 감사드립니다.

답변

2

이렇게 늦게 바인딩 된 COM을 작성해야합니다. 나쁜 옛날에 클라이언트 코드. 그것을 얻기위한 상당한 고통과 고통, 스 니펫에있는 것이 아직 가까이 있지 않습니다. 먼저이 작업을 수행하는 다른 방법을 제안 할 것이고, 이러한 COM 개체는 모든 Windows 버전에서 사용할 수 있으므로 더 이상 변경하지 않으므로 늦게 바인딩하는 것이 좋습니다. VS2010 이후 지원되는 "Interop Interp Types"기능은 피할 수있는 타당한 이유를 제거합니다.

프로젝트> 참조 추가> COM 탭. "Microsoft 인터넷 제어"및 "Microsoft 셸 제어 및 자동화"를 선택하십시오. 이제 당신이 인텔리의 모든 혜택과 함께, 바인딩 - 초기 좋은 컴팩트 올바른 구성원을 찾아 오타 방지에 쓸 수 있습니다

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

약간 바보 예를 들어,이 경로가 표시되는 모든 Explorer 창을 새로 고침 C 드라이브에 있습니다. Path 속성은 LocationURL이 필요하다는 것을 발견하는 데 유용하지 않습니다. IE가 디렉토리 내용을 표시 할 수 있기는하지만 Internet Explorer와 Windows 탐색기 윈도우 (일명 "파일 탐색기") 사이의 구별을 찾아야 할 수도 있습니다. 그래서 이것이 가장 정확한 버전이라고 생각합니다.

이 후기 바인딩을 사용하려는 경우 고통을 최소화하려면 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"을 명시 적으로 사용하십시오.

+0

감사합니다. 선생님, 15 번 받으면 upvote하겠습니다. – Learning

관련 문제