0

我设法使用来自http://mwinapi.sourceforge.net/的 SystemAccessibleObject 从 Windows 资源管理器获取当前工作文件夹中的选定文件

我想获取带有扩展名的文件名,但如果您启用“隐藏已知文件类型的扩展名”,那么将只有文件名。我被困在这一步。

我的代码:

SystemAccessibleObject currentWorkingWindow = SystemAccessibleObject.FromWindow(new SystemWindow(GetForegroundWindow()), AccessibleObjectID.OBJID_WINDOW).Children.Where(c => c.RoleString.Equals("client")).FirstOrDefault();

            if (null != currentWorkingWindow)
            {
                SystemWindow addressBar = currentWorkingWindow.Window.AllDescendantWindows.Where(w => w.ClassName.Equals("ComboBox")).FirstOrDefault();

                if (null != addressBar)
                {
                    string addressBarContent = addressBar.Content.LongDescription;

                    Match m = Regex.Match(addressBarContent, "Address \\[push button\\]\\n[A-Z]: \\n[A-Z]: ([A-Z]:\\\\[^/:*?<>|\"\\r\\n\\t]+)");

                    if (null != m || null != m.Groups[1])
                    {
                        SystemWindow currentListView = currentWorkingWindow.Window.AllDescendantWindows.Where(w => w.ClassName.Equals("SysListView32")).FirstOrDefault();

                        if (null != currentListView)
                        {
                            SystemAccessibleObject currentListViewItems = SystemAccessibleObject.FromWindow(currentListView, AccessibleObjectID.OBJID_WINDOW).Children.Where(c => c.RoleString.Equals("list")).FirstOrDefault();

                            if (null != currentListViewItems)
                            {
                                SystemAccessibleObject[] selectedItems = currentListViewItems.SelectedObjects;

                                string currentWorkingFolderPath = m.Groups[1].Value;

                                if (0 != selectedItems.Count())
                                {
                                    string[] fileNames = currentListView.Content.PropertyList.Select(p => p.Value).ToArray();
                                }
                            }
                        }

                        currentListView = null;
                    }

                    m = null;
                }

                addressBar = null;
            }

            currentWorkingWindow = null;

任何帮助将不胜感激!

4

2 回答 2

0

我对 SystemAccessibleObject 一无所知,但为什么不从不同的角度来解决这个问题。您知道目录和不带扩展名的文件名,因此您可以使用Directory.GetFiles并“查找”带扩展名的文件。

foreach (string fileName in fileNames)
{
  string[] matchedFiles = Directory.GetFiles(currentWorkingFolderPath, fileName+"*");
  etc...
}
于 2010-10-13T15:42:26.090 回答
0

我还没有找到完美的解决方案,但使用剪贴板并不是一个坏主意。

于 2010-11-05T04:34:16.817 回答