2016-04-27 88 views
1

儘管之前已經提出過這個問題,但是在答案中提出的代碼並沒有讓我更進一步,所以也許有人可以闡明這一點。通過C中的句柄檢索資源管理器窗口的完整路徑#

我從this得到了大部分代碼回答了問題。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.Threading.Tasks; 

namespace TestClassLibrary 
{ 

    public class ClassTest 
    { 

    public ClassTest() 
    { 
    } 

    public static void GetWindowPath(int handle, out string paths) { 

     SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); 
     var explorer = shellWindows.Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault(); 
     if (explorer != null) 
     { 
      string path = new Uri(explorer.LocationURL).LocalPath; 
      Console.WriteLine("name={0}, path={1}", explorer.LocationName, path); 
      paths = path; 
     } 

     else 
     { 
      //Console.WriteLine("name={0}, path={1}", explorer.LocationName, path); 
      paths = "Test"; 
     } 
    } 

基本上我想實現的是讓這個方法返回與句柄對應的窗口的完整路徑。該方法稍後將在外部使用該代碼編譯到的.DLL。

我遇到的問題是由於某種原因,資源管理器始終爲NULL,因此 不返回路徑。

如果有人能夠對此有所瞭解,我會很高興,以便我和其他可能在此問題上遇到問題的人可能知道該怎麼做。

+0

提供哪些將您的應用程序做更長遠的路?如果你希望它可以作爲上下文菜單等,你點擊的文件將發送完整的路徑..因此,這可能是不必要的一些 – BugFinder

+0

啊,基本上我需要實現它到我們的實際編程語言,Windev,我可以通過將其加載爲.dll來實現。長遠來說,我想實現從程序拖放到資源管理器,但不是通過C#,而是通過Windev。 – FBC

+0

嗯,不是我嘗試過的區域..然而,我會想象,只要被拖動的項目被設置爲聲明其文件等,explorer就會看到它,就像vmware允許你從os之外拖放到vm等內部。 – BugFinder

回答

0

感謝shlatchz回答和類似主題的各種方法,我找到了一種方法來獲取通過窗口句柄的路徑。這或多或少都是它們的組合,我希望它能幫助人們在未來準確地尋找這一點。

 //Method to obtain the window path that corresponds to the handle passed 
    public static string GetWindowPath(int handle) 
    { 
     //Default value for the variable returned at the end of the method if no path was found 
     String Path = ""; 


     SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); 


     foreach (SHDocVw.InternetExplorer window in shellWindows) 
     { 

      //If Window handle corresponds to passed handle 
      if (window.HWND == handle) 
      { 
       //Print path of the respective folder and save it within the returned variable 
       Console.WriteLine(window.LocationURL); 
       Path = window.LocationURL; 
       break; 
      } 

     } 

     //If a path was found, retun the path, otherwise return "" 
     return Path; 



    } 

返回將是相同的格式shlatchz答案file:///C:/Program%20Files/...

1

這是我如何打印每個瀏覽器窗口的目錄:

public void RefreshWindow() 
{ 
    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); 
    Parallel.For(0, (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" || itemName == "File Explorer") 
     { 
      string currDirectory = (string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null); 
      Console.WriteLine(currDirectory); 
     } 
    }); 
} 

地址格式爲file:///C:/Program%20Files/...

您可以分析這個地址的格式爲你想要的(通過消除file:///前綴和\更換/)&也解碼HTML encoding

+0

nice :)可能值得給出一個輸出示例 - I注意它是一個'file:'uri。 –

+0

謝謝你的回答!我試圖在控制檯應用程序中實現這個功能來測試輸出,但由於某些原因,由於itemname爲null,它不會運行到最終的「if」中。我是否有任何錯誤使用它? – FBC

+0

沒關係,我的例子中的itemname是「Windows-Explorer」而不是「Windows Explorer」,它實際上打印每個資源管理器窗口的目錄,完全按照它的樣子。現在唯一的問題是我如何通過窗口句柄獲得一個路徑。 – FBC

相關問題