从 AccessibleObjectFromWindow 获取对象扩展?

发布于 2024-09-27 09:57:30 字数 2398 浏览 2 评论 0原文

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

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

我的代码:

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;

任何帮助将不胜感激!

I managed to get the selected file in the current working folder from Windows Explorer using SystemAccessibleObject from http://mwinapi.sourceforge.net/

I want to get the filename with extension but if you enable "Hide extensions for known file types" then there will be only the filename. I'm stuck on this step.

My code:

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;

Any helps would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-10-04 09:57:30

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

foreach (string fileName in fileNames)
{
  string[] matchedFiles = Directory.GetFiles(currentWorkingFolderPath, fileName+"*");
  etc...
}

I do not know anything about the SystemAccessibleObject but why not approach this from a different angle. You know the directory and the file name without extension, so you could use the Directory.GetFiles and "find" your file with extension.

foreach (string fileName in fileNames)
{
  string[] matchedFiles = Directory.GetFiles(currentWorkingFolderPath, fileName+"*");
  etc...
}
一花一树开 2024-10-04 09:57:30

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

I didn't figure out the perfect solution yet but using Clipboard is not a very bad idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文