2010/09/25(Sat) 15:15:57 編集(投稿者)
dynamic 使えない場合はこんな感じ。
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const string ExplorerFullPath = @"%WINDIR%\explorer.exe";
string explorerExpandedPath
= Environment.ExpandEnvironmentVariables(ExplorerFullPath);
Type shellType = Type.GetTypeFromProgID("Shell.Application");
object shell = null; // Shell32.Shell
try
{
shell = Activator.CreateInstance(shellType);
object windows = null; // SHDocVw.IShellWindows
try
{
windows
= shellType.InvokeMember(
"Windows"
, BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public
, null
, shell
, null);
Type windowsType = windows.GetType();
int windowCounts
= (int)windowsType.InvokeMember(
"Count"
, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public
, null
, windows
, null);
object window = null; // SHDocVw.IWebBrowser2
for (int i = 0; i < windowCounts; ++i)
{
try
{
window
= windowsType.InvokeMember(
"Item"
, BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public
, null
, windows
, new object[] { i });
Type windowType = window.GetType();
string processFullPath
= (string)windowType.InvokeMember(
"FullName"
, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public
, null
, window
, null);
if (explorerExpandedPath.Equals(
processFullPath, StringComparison.InvariantCultureIgnoreCase))
{
string currentDirectory
= (string)windowType.InvokeMember(
"LocationUrl"
, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public
, null
, window
, null);
Console.WriteLine(Uri.UnescapeDataString(currentDirectory));
}
}
finally
{
if (window != null) { Marshal.ReleaseComObject(window); }
}
}
}
finally
{
if (windows != null) { Marshal.ReleaseComObject(windows); }
}
}
finally
{
if (shell != null) { Marshal.ReleaseComObject(shell); }
}
}
}
}
型名 comments 付与。