using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Forms;
class Program
{
private static string Folder = "";
private static IntPtr fld;
private static uint psfgaoOut;
private static void OnWindowOpened(object src, AutomationEventArgs e)
{
try
{
var elem = src as AutomationElement;
Process proc = Process.GetProcessById(elem.Current.ProcessId);
if (elem != null && proc.ProcessName == "explorer")
{
WindowPattern windowPattern = null;
windowPattern = elem.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
if (windowPattern.WaitForInputIdle(4000))
{
Thread.Sleep(200);
for (int i=10; i>0; i--)
{
Thread.Sleep(100);
if (Array.Exists(new string[] { "Administrative Tools",
"Control Panel\\All Control Panel Items\\Administrative Tools",
"Windows Tools",
"Control Panel\\All Control Panel Items\\Windows Tools" },
element => element == elem.Current.Name))
{
SetForegroundWindow((IntPtr)elem.Current.NativeWindowHandle);
Thread.Sleep(250);
if (Folder != "")
{
if (fld != IntPtr.Zero)
{
SendKeys.SendWait("^{l}\\{Enter}");
SHOpenFolderAndSelectItems(fld, 0, new IntPtr[] {}, 0);
SendKeys.SendWait("{Enter}");
}
else
{
SendKeys.SendWait("^{l}" + Folder + "{Enter}");
}
}
else
{
SendKeys.SendWait("^{l}\\{Enter}");
}
Process.GetCurrentProcess().Kill();
}
}
}
}
}
catch
{
Process.GetCurrentProcess().Kill();
}
}
static void Main(string[] args)
{
if (args.Length > 0)
{
Folder = args[0].TrimEnd(new [] {'\\', '\"'}).TrimStart(new [] {'\"'});
if (Folder.EndsWith(":"))
{
Folder = Folder + "\\";
}
try
{
if (new DirectoryInfo(Folder).Parent != null)
{
SHParseDisplayName(Path.GetFullPath(Folder), IntPtr.Zero, out fld, 0, out psfgaoOut);
if (fld == IntPtr.Zero)
{
Process.GetCurrentProcess().Kill();
}
}
}
catch
{
}
}
System.Windows.Automation.Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children,
OnWindowOpened);
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "control.exe",
Arguments = "admintools",
RedirectStandardOutput = false,
RedirectStandardError = false,
UseShellExecute = true,
CreateNoWindow = false
};
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
// Wait
Thread.Sleep(10000);
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("shell32.dll")]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags);
[DllImport("shell32.dll")]
private static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, out IntPtr pidl, uint sfgaoIn, out uint psfgaoOut);
}