获取路径任何Windows应用程序中打开文档的文件名 [英] Getting the path & filename of the open document in any Windows application

查看:120
本文介绍了获取路径任何Windows应用程序中打开文档的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,我感兴趣的是如何执行此操作的特定部分:如何获取任何当前正在运行的Windows应用程序的当前打开文档"的路径和文件名(如果使用任何 Windows应用程序无法做到这一点,那么,下一个最好的选择就是让它可以与尽可能多的应用程序一起使用.)

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.)

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

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.

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

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.

一个想法:能否以某种方式利用最近的文档"文件夹(和/或其底层的Windows API)来提供帮助?似乎具有与我在此处感兴趣的开放文档"概念相同的信息,这些信息显然适用于各种应用程序类型. (查看我机器上的最近使用的文档"文件夹的内容,我发现其中的条目显然是我使用各种应用程序(包括MS Word,MS Excel,Eclipse,Adobe Acrobat Reader,Paint.NET,TOAD,和记事本2.)

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.)

我希望使用C#或C ++代码实现首选解决方案,但是无论实现语言如何,我都愿意接受有关如何执行此操作的任何建议!

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!

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

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?

推荐答案

您可能要做的最好的事情就是查看最新文档的注册表项,并获取最新文档的列表.在此CodeProject文章中,有一些用于处理此数据的示例代码.它将保存在:

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.

这篇关于获取路径任何Windows应用程序中打开文档的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆