我想知道如何使用C#获取当前活动窗口(即具有焦点的窗口)的Window标题.
是否可以从我的C#应用程序获取当前在Windows资源管理器中选择的文件列表?
我已经对从C#等托管语言与Windows资源管理器交互的不同方法做了大量研究.最初,我正在研究shell扩展的实现(这里和这里例如),但显然这是托管代码中的一个坏主意,并且对我的情况来说可能有点过分.
接下来,我查看了PInvoke/COM解决方案,并发现了这篇文章,它让我得到了这段代码:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
string filename;
ArrayList windows = new ArrayList();
foreach(SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if(filename.Equals("explorer"))
{
Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
windows.Add(ie);
var shell = new Shell32.Shell();
foreach (SHDocVw.InternetExplorerMedium sw in shell.Windows())
{
Console.WriteLine(sw.LocationURL);
}
}
}
Run Code Online (Sandbox Code Playgroud)
...但是单个InternetExplorer对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息.
然后我发现这篇文章正是我所需要的,但是在C++中.以此为出发点,我试图通过shell32.dll在项目中添加作为参考来进行一些翻译.我最终得到了以下内容:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
string filename;
ArrayList windows = new ArrayList();
foreach(SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if(filename.Equals("explorer")) …Run Code Online (Sandbox Code Playgroud) 嘿伙计们,我试图获取用户正在使用的文件夹的选定文件.我有以下代码已经运行,但只在桌面文件上:
private string selectedFiles()
{
// get the handle of the desktop listview
IntPtr vHandle = WinApiWrapper.FindWindow("Progman", "Program Manager");
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
//IntPtr vHandle = WinApiWrapper.GetForegroundWindow();
//Get total count of the icons on the desktop
int vItemCount = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMCOUNT, 0, 0);
//MessageBox.Show(vItemCount.ToString());
uint vProcessId;
WinApiWrapper.GetWindowThreadProcessId(vHandle, out vProcessId);
IntPtr vProcess = WinApiWrapper.OpenProcess(WinApiWrapper.PROCESS_VM_OPERATION | WinApiWrapper.PROCESS_VM_READ |
WinApiWrapper.PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = WinApiWrapper.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
WinApiWrapper.MEM_RESERVE | WinApiWrapper.MEM_COMMIT, WinApiWrapper.PAGE_READWRITE);
try
{
for …Run Code Online (Sandbox Code Playgroud)