C++ 如何使用与其文件扩展名关联的程序打开文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9115574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:18:31  来源:igfitidea点击:

How can you open a file with the program associated with its file extension?

c++windows

提问by Valmond

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)

有没有一种简单的方法可以通过 Windows 中的关联程序打开文件?(就像在 Windows 资源管理器中双击它但使用我的代码自动完成)

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.

例如,在计算机 A 上,“text.txt”将在写字板中打开,但在计算机 B 上,由于用户文件扩展名分配,它将由 Notepad++ 打开。

I tried ShellExecute

我试过 ShellExecute

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

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

这有效,但如果我省略 notepad.exe 参数会发生奇怪的事情(显示随机资源管理器)。

回答by Mark Ransom

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

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

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

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

通过将动词保留为 NULL (0) 而不是L"open",您将获得文件类型的真正默认操作 - 通常是open但并非总是如此。

回答by Sebastian Mach

See Launching Applications:

请参阅启动应用程序

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

On windows, a good memory hook is to think of all data-files being executable by the shell. You can also try it out in a command box, where you can just type a filename, and it will be opened up. Or, the other way around, every file in Windows can be opened, and the default opening-action for executable files is to execute them.

在 Windows 上,一个好的内存挂钩是认为所有数据文件都可以由 shell 执行。您也可以在命令框中尝试一下,您只需在其中键入文件名,它就会被打开。或者,反过来说,Windows 中的每个文件都可以打开,可执行文件的默认打开操作是执行它们。

回答by Frank Schmitt

According to the MS Knowledge Base, ShellExecute should work (we do this in Delphi all the time):

根据MS 知识库,ShellExecute 应该可以工作(我们一直在 Delphi 中这样做):

ShellExecute(Handle, "Open", Filename, "", "C:\", SW_SHOWNORMAL)

回答by Blacktempel

A little more possibilities here:

这里有更多的可能性:

If you want to open - for example - the file by default with Notepad++ (if installed), you could scan for it's registry key if it exists and where it is, (Usually HKLM\SOFTWARE\Wow6432Node\Notepad++[tested Win7]) then take that path and open it.

如果您想打开 - 例如 - 默认情况下使用 Notepad++(如果已安装)打开该文件,您可以扫描它的注册表项(如果它存在)以及它的位置(通常HKLM\SOFTWARE\Wow6432Node\Notepad++[tested Win7]),然后采用该路径并打开它。

std::wstring file = L"C:\\Outfile.txt";

std::wstring file = L"C:\\Outfile.txt";

if (NotepadPlusPlusExists()) //Open with Notepad++ or use an other program... (maybe your own ?)
{
    std::wstring wsNPPPath = GetNotepadPlusPlusPath();
    ShellExecuteW(HWND, L"open", wsNPPPath.c_str(), file.c_str(), NULL, SW_NORMAL);
}
else //Open with default associated program <---
    ShellExecuteW(HWND, NULL, file.c_str(), NULL, NULL, SW_NORMAL);

If you want the user to be able to change the default program or select a program he/she wants to use, you may open the "Open with" dialog.

如果您希望用户能够更改默认程序或选择他/她想要使用的程序,您可以打开“打开方式”对话框。

//std::wstring StringArgsW(const wchar_t *format, ...);
std::wstring wsCmdOpenWith = StringArgsW(L"C:\Windows\system32\shell32.dll,OpenAs_RunDLL \"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"C:\Windows\system32\rundll32.exe", wsCmdOpenWith.c_str(), NULL, SW_NORMAL);

You can also open the file in explorer.

您也可以在资源管理器中打开该文件。

std::wstring wsCmdExplorer = StringArgsW(L"/select,\"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"explorer.exe", wsCmdExplorer.c_str(), NULL, SW_NORMAL);

回答by Alex K.

If lpFile specifies a document file, the flag is simply passed to the associated application

如果 lpFile 指定了一个文档文件,则该标志被简单地传递给相关的应用程序

So you need to substitute "c:\\windows\\notepad.exe"with the actual file you want to open and leave lpParametersnull.

所以你需要"c:\\windows\\notepad.exe"用你想要打开的实际文件替换并保留lpParameters空值。

回答by mcnicholls

Maybe try startinstead of open?

也许尝试start而不是open