。如何打开一个文件和程序相关联的文件扩展名? [英] How can you open a file with the program associated with its file extension?

查看:97
本文介绍了。如何打开一个文件和程序相关联的文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来打开一个文件通过其相关联的程序在Windows中?
(像在Windows资源管理器中双击,但是用我的代码自动完成)

Is there a simple way to open a file by its associated program in windows? (like double clicking it in windows explorer but done automatically with my code)

例如,在计算机A上,text.txt

For example, on computer A, "text.txt" will be opened in wordpad but on computer B it will be opened by Notepad++ because of the users file extension assignments.

我尝试使用ShellExecute

I tried ShellExecute

ShellExecute(0, L"open", L"c:\\windows\\notepad.exe" ,L"c:\\outfile.txt" , 0 , SW_SHOW );

这可以工作,但如果我省略notepad.exe参数奇怪的事情发生(显示一个随机的资源管理器) 。

which works but if I omit the notepad.exe parameter weird things happen (a random explorer is shown).

推荐答案

要使用文件作为文件参数打开,而不是参数参数。无需指定要使用的程序, ShellExecute 将为您查找。

You want to use the file to open as the file argument, not the parameter argument. No need to specify which program to use, ShellExecute will look it up for you.

ShellExecute(0, 0, L"c:\\outfile.txt", 0, 0 , SW_SHOW );

通过将动词留为NULL(0)而不是 Lopen ,您将获得文件类型的真正默认操作 - 通常是打开,但不是始终。

By leaving the verb as NULL (0) rather than L"open", you get the true default action for the file type - usually this is open but not always.

这篇关于。如何打开一个文件和程序相关联的文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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