Created
August 9, 2019 09:22
-
-
Save KageShiron/2328b2567385314a8d79b71fb11eb029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MicroBatchFramework; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ExplorerUtils | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BatchHost.CreateDefaultBuilder().RunBatchEngineAsync<ExplorerBatch>(args); | |
} | |
class ShellWindow | |
{ | |
public ShellWindow( string uri, long hwnd) | |
{ | |
this.Uri = uri; | |
this.Hwnd = new IntPtr(hwnd); | |
} | |
public string Uri{get;set;} | |
public IntPtr Hwnd { get; set; } | |
} | |
public class ExplorerBatch : BatchBase | |
{ | |
static readonly Guid CLSID_ShellWindows = new Guid("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}"); | |
public void Default() | |
{ | |
Last(); | |
} | |
[Command("last","Last actived explorer")] | |
public void Last( | |
[Option("l", "no output if last actived window have no filesystem path.")] bool lastOnly = false) | |
{ | |
dynamic shWin = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_ShellWindows)); | |
var wins = new ShellWindow[shWin.Count]; | |
int i = 0; | |
foreach (dynamic w in shWin) | |
{ | |
wins[i++] = new ShellWindow(w.LocationURL, w.HWND); | |
} | |
PInvoke.User32.EnumWindows((hwnd, _) => | |
{ | |
foreach (var w in wins) | |
{ | |
if (w.Hwnd == hwnd) | |
{ | |
if (w.Uri == "") | |
{ | |
if (lastOnly) | |
{ | |
Environment.Exit(1); | |
} | |
else { continue; } | |
} | |
Console.WriteLine(new Uri(w.Uri).LocalPath); | |
return false; | |
} | |
} | |
return true; | |
}, IntPtr.Zero); | |
} | |
[Command("list","Show all explorer paths")] | |
public void List( | |
[Option("l", "no output if last actived window have no filesystem path.")] bool lastOnly = false) | |
{ | |
dynamic wins = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_ShellWindows)); | |
foreach (dynamic w in wins) | |
{ | |
Console.WriteLine(new Uri(w.LocationURL).LocalPath); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment