获取路径& 任何 Windows 应用程序中打开文档的文件名

发布于 2024-08-01 21:24:37 字数 1360 浏览 4 评论 0原文

目标

让我从我首先希望能够做的事情的最终愿景开始:在 Windows 中,我希望能够使用我定义的全局键盘快捷键(例如,Ctrl+Alt+C) 将前台应用程序中打开的文档的完整路径和文件名复制到剪贴板。

例如,这对于随后能够粘贴路径和路径将很有用。 文件名放入电子邮件客户端中的“打开文件”对话框中,以将该文档附加到电子邮件中,而无需手动浏览到文件系统中的目标文档。

具体问题

现在,我感兴趣的具体部分是如何实现:如何获取任意当前运行的 Windows 应用程序的当前“打开文档”的路径和文件名。 (如果任何 Windows 应用程序都无法做到这一点,那么下一个最好的办法就是让它与尽可能多的应用程序一起工作。)

显然,这不适用某些应用程序不一定具有与本地文件系统上的文件相对应的“当前打开的文档”的概念,例如电子邮件客户端、IM 客户端或(通常)Web 浏览器。

特定于应用程序的解决方案

我知道可以编写特定于应用程序的解决方案来执行此操作。 例如,以下 MS Word VBA 子例程会将 Word 中打开的文档的文件名和路径复制到剪贴板:

Dim myDataObject As DataObject
Set myDataObject = New DataObject
myDataObject.SetText ActiveDocument.FullName
myDataObject.PutInClipboard

但是,我真正想要的是适用于我系统上的任何应用程序的东西(或者,再次,适用于尽可能多的解决方案),而不必尝试为每个解决方案编写特定于应用程序的解决方案。

想法:最近的文档文件夹

一个想法:是否可以利用“最近的文档”文件夹(和/或其底层 Windows API)以某种方式来帮助解决此问题? 它似乎有关于我在这里感兴趣的“开放文档”相同概念的信息,这显然适用于各种应用程序类型。 (查看我计算机上“最近的文档”文件夹的内容,我看到其中的条目显然是为我使用各种应用程序打开的文档创建的,包括 MS Word、MS Excel、Eclipse、Adobe Acrobat Reader、Paint.NET、TOAD、和 Notepad2。)

首选解决方案语言

更喜欢 C# 或 C++ 代码中的解决方案,但我愿意接受任何有关如何执行此操作的建议,无论实现语言如何!

Windows 7的?

更新 11/2009: 既然 Windows 7 已广泛使用,我认为可能值得回到这个问题并询问:Windows 7 是否提供任何新的 API 或任何其他机制,这将有所帮助我想在这里完成什么?

Goal

Let me start with my final vision of what I'd like to be able to do first: In Windows, I'd like to be able to use a global keyboard shortcut that I define (say, Ctrl+Alt+C) to copy the full path and filename of the open document in the foreground application to the clipboard.

This would be useful to, for example, be able to subsequently paste the path & filename into an "Open File" dialog in an email client to attach that document to an email, without having to manually browse to the target document in the filesystem.

Specific Question

Now, the specific part of how to do this that I'm interested in how to implement is: How can I get the path and filename of the current "open document" of any arbitrary currently-running Windows application. (If this can't be done with any Windows application, then the next best thing would be for this to work with as many applications as possible.)

Obviously, this wouldn't apply to some applications that don't necessarily have the concept of a "currently open document" that corresponds to a file on the local filesystem, such as an email client, an IM client, or (usually) a web browser.

Application-Specific Solutions

I'm aware that it's possible to write application-specific solutions to do this. For example, the following MS Word VBA subroutine will copy the filename and path of the open document in Word to the clipboard:

Dim myDataObject As DataObject
Set myDataObject = New DataObject
myDataObject.SetText ActiveDocument.FullName
myDataObject.PutInClipboard

However, what I really want is something that will work for any of the applications on my system (or, again, for as many of them as reasonably possible) without having to try and write an application-specific solution for each one.

Idea: Recent Documents Folder

One idea: Could the Recent Documents folder (and/or its underlying Windows APIs) somehow be leveraged to help with this? It seems to have information about the same concept of "open documents" that I'm interested in here, that apparently applies across various application types. (Looking at the contents of the Recent Documents folder on my machine, I see entries in there that were apparently made for documents that I opened with various applications including MS Word, MS Excel, Eclipse, Adobe Acrobat Reader, Paint.NET, TOAD, and Notepad2.)

Preferred Solution Language

I'd prefer solutions in C# or C++ code, but I'm open to any suggestions for how to go about doing this, regardless of implementation language!

Windows 7?

Update 11/2009: Now that Windows 7 is widely available, I figured it might be worth coming back to this question and asking: Does Windows 7 provide any new APIs, or any other mechanism, that would help with what I'm trying to accomplish here?

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

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

发布评论

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

评论(2

已下线请稍等 2024-08-08 21:24:37

您可能做的最好的事情就是查看最近的文档注册表项,并获取最新文档的列表。 这篇 CodeProject 文章中提供了一些处理此数据的示例代码。 它保存在:

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs

但是,这不会显示文档当前是否打开。 您可能会检查所有打开的应用程序的标题,因为许多应用程序将文档名称放在其窗口标题中,但这不是必需的,并且许多应用程序不这样做。

The best you could probably do is look at the recent documentation registry keys, and get the list of most recent documents. Some sample code for working with this data is in this CodeProject article. This is saved in:

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs

However, this isn't going to show you whether a document is currently open or not. You could potentially check the title of all open applications, since many applications put document names in their window titles, but this is not a requirement, and many applications do not do that.

横笛休吹塞上声 2024-08-08 21:24:37

应用程序没有强制机制来指定其打开的文档,因此这通常是不可能的。

There is no mandatory mechanism for an application to specify its open document, so this is not generically possible.

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