@@ -14,7 +14,7 @@ public class FileExplorerHelper
14
14
public static string GetActiveExplorerPath ( )
15
15
{
16
16
var explorerWindow = GetActiveExplorer ( ) ;
17
- string locationUrl = explorerWindow . LocationURL ;
17
+ string locationUrl = explorerWindow ? . LocationURL ;
18
18
if ( ! string . IsNullOrEmpty ( locationUrl ) )
19
19
{
20
20
return new Uri ( locationUrl ) . LocalPath ;
@@ -28,31 +28,25 @@ public static string GetActiveExplorerPath()
28
28
/// <summary>
29
29
/// Gets the file explorer that is currently in the foreground
30
30
/// </summary>
31
- private static SHDocVw . InternetExplorer GetActiveExplorer ( )
31
+ private static dynamic GetActiveExplorer ( )
32
32
{
33
33
// get the active window
34
34
IntPtr handle = GetForegroundWindow ( ) ;
35
35
36
- // Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
37
- var shellWindows = new SHDocVw . ShellWindows ( ) ;
38
-
39
- // loop through all windows
40
- foreach ( var window in shellWindows )
36
+ Type type = Type . GetTypeFromProgID ( "Shell.Application" ) ;
37
+ if ( type == null ) return null ;
38
+ dynamic shell = Activator . CreateInstance ( type ) ;
39
+ var openWindows = shell . Windows ( ) ;
40
+ for ( int i = 0 ; i < openWindows . Count ; i ++ )
41
41
{
42
- if ( window is SHDocVw . InternetExplorer explorerWindow && new IntPtr ( explorerWindow . HWND ) == handle )
43
- {
44
- // we have found the desired window, now let's make sure that it is indeed a file explorer
45
- // we don't want the Internet Explorer or the classic control panel
46
- if ( explorerWindow . Document is not Shell32 . IShellFolderViewDual2 )
47
- {
48
- return null ;
49
- }
50
- if ( Path . GetFileName ( explorerWindow . FullName ) != "explorer.exe" )
51
- {
52
- return null ;
53
- }
42
+ var window = openWindows . Item ( i ) ;
43
+ if ( window == null ) continue ;
54
44
55
- return explorerWindow ;
45
+ // find the desired window and make sure that it is indeed a file explorer
46
+ // we don't want the Internet Explorer or the classic control panel
47
+ if ( Path . GetFileName ( ( string ) window . FullName ) == "explorer.exe" && new IntPtr ( window . HWND ) == handle )
48
+ {
49
+ return window ;
56
50
}
57
51
}
58
52
0 commit comments