如何找出打开具有自定义文件扩展名的文件所触发的文件的路径?
如何获取用于打开程序的文件的位置?
示例:如果我创建一个新的扩展名“.xyz”,并且我告诉 Windows 我想使用 myapplication 打开文件类型 .xyz,那么它就会启动我的应用程序。 很好,但是我的应用程序如何获取用于启动它的文件的文件路径的句柄?
另外,有没有一种方法可以让我的应用程序仅运行一个版本,并打开新文件以仅调用我的应用程序中的方法? 例如,如果您使用 torrent 并且打开 5 个 .torrent 文件,它们都会传递到一个应用程序。
附带问题:所有文件扩展名都是 3 个字母长吗?是否有公开使用的文件扩展名列表? 如果我要创建文件扩展名,我不想使用已经使用过的文件扩展名。
How do i get the location of the file that i used to open my programs with?
Example: if i create a new extention ".xyz" say and i tell windows that i want to open the file type .xyz with myapplication, then it starts my aplication. Great, but how does my application get a handle on the file path of the file that was used to start it?
Also, is there a way to keep just one version of my app running and new files that are opened to just call a method in my application? For example if your using a torrent and you open 5 .torrent files they all just get passed to one application.
Side question: are all file extensions 3 letters long and is there a list of ones that are publicly used? If im creating a file extension I don't want to use one that is already used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建文件关联时,您指定了资源管理器应运行以激活程序的命令行。 shell 也会将文档文件的名称放在命令行上,因此在您的程序中,检查命令行参数。 如何执行此操作取决于您的语言和开发环境。 在 Delphi 中,使用
ParamCount
和ParamStr
函数。创建文件关联时,您可以在命令行上准确指定文档文件名的位置。 在命令行中的某个位置使用
%1
,shell 会将其替换为文件名。 由于 Windows 文件名经常包含空格,因此您应该在文件名两边加上引号,因此文件关联中的命令行将如下所示:通过该关联,双击另一个文档文件将启动程序的另一个实例。 如果您希望在已运行实例的另一个窗口中打开文档,则可以编写代码以使程序在启动时查找已运行实例。 如果它找到一个,那么它可以与该实例通信,告诉它要打开哪个文件。 您可以通过多种方式实现通信,包括邮槽、套接字、命名管道、内存映射文件和 DDE。
shell 的文件关联机制已经有一种通过 DDE 进行通信的方式,因此程序的第二个实例根本不会启动。 相反,shell 会启动与已经运行的实例的 DDE 对话,并以这种方式告诉它新的文件名。 然而,DDE 如今似乎已经不再受欢迎,因此请先查看其他一些选项。
对于你的附带问题,不,扩展名并不总是三个字符长。 环顾四周,这应该是显而易见的:C 代码放在 .c 文件中,Adobe Illustrator 图形放在 .ai 文件中,新的 Microsoft Word 文档放在 中.docx 文件。
但要小心。 如果您要求 **.doc*,结果也将包含 .docx 文件。 这是因为
FindFirstFile
匹配短文件名和长文件名,并且具有长文件扩展名的长文件名在其短文件名版本中具有三个字符的扩展名。When you created your file association, you specified the command line that Explorer should run to activate your program. The shell puts the name of the document file on the command line, too, so in your program, check the command-line arguments. How you do that depends on your language and development environment. In Delphi, use the
ParamCount
andParamStr
functions.When you create the file association, you can specify exactly where on the command line the document file name should go. Use
%1
somewhere on the command line, and the shell will replace it with the file name. Since Windows file names frequently contains spaces, you should put quotation marks around the file name, so the command line in the file association would look like this:With that association, double-clicking another document file will start another instance of your program. If you'd prefer to have the document opened in another window of the already-running instance, then you can write code to make your program look for already-running instances when it starts up. If it finds one, then it can communicate with that instance to tell it what file to open. You can effect that communication any number of ways, including mailslots, sockets, named pipes, memory-mapped files, and DDE.
The shell's file-association mechanism already has a way of communicating via DDE, so a second instance of your program wouldn't be started at all. Instead, the shell would start a DDE conversation with the already-running instance and tell it the new file name that way. However, DDE seems to be falling out of favor nowadays, so check out some of the other options first.
For your side question, no, extensions are not always three characters long. Look around, and that should be obvious: C code goes in .c files, Adobe Illustrator graphics go in .ai files, and new Microsoft Word documents go in .docx files.
But beware. If you ask for **.doc*, the results will include .docx files as well. That's because
FindFirstFile
matches both short and long file names, and long file names with long file extensions have three-character extensions in their short-file-name versions.罗布完美地回答了你的问题。
至于最后一部分,是否存在文件扩展名的公共列表 - 不是这样,但有 shell.windows.com,Web 服务资源管理器用于查找未知文件扩展名的处理程序。 您可以创建一个扩展,然后查询 shell.windows.com 以查看它是否已注册。 例如,要检查扩展名
.blah
是否已被任何人在 shell.windows.com 上注册,只需在任何浏览器中打开此 URL:http://shell.windows.com/fileassoc/0409/xml/redir.asp?ext=blah
当然,请将结尾的
blah
替换为您的扩展名。您可以在 KB929149 和 Raymond Chen 的帖子 shell.windows.com 从哪里获取有关文件扩展名的信息以及如何获取那个动作?。
Rob covered the answer to your question(s) beautifully.
As to the last part, whether there is a public list of file extensions - not as such, but there is shell.windows.com, the web service Explorer uses to locate handlers for unknown file extensions. You can make up an extension then query shell.windows.com to see whether it's been registered. For example, to check whether the extension
.blah
has been registered by anyone on shell.windows.com, just open this URL in any browser:http://shell.windows.com/fileassoc/0409/xml/redir.asp?ext=blah
Of course, replace the trailing
blah
with your extension.You can find more details about this in KB929149 and in Raymond Chen's post Where does shell.windows.com get information about file extensions, and how do I get in on that action?.