Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何打开与文件扩展名关联的程序的文件?_C++_Windows - Fatal编程技术网

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

C++ 如何打开与文件扩展名关联的程序的文件?,c++,windows,C++,Windows,有没有一种简单的方法可以在windows中通过相关程序打开文件? (类似于在windows资源管理器中双击它,但使用“我的代码”自动完成) 例如,在计算机A上,“text.txt”将在写字板中打开,但在计算机B上,由于用户分配了文件扩展名,它将由Notepad++打开 我试过了 ShellExecute(0, L"open", L"c:\\windows\\notepad.exe" ,L"c:\\outfile.txt" , 0 , SW_SHOW ); 如果我忽略notepad.exe参数,

有没有一种简单的方法可以在windows中通过相关程序打开文件? (类似于在windows资源管理器中双击它,但使用“我的代码”自动完成)

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

我试过了

ShellExecute(0, L"open", L"c:\\windows\\notepad.exe" ,L"c:\\outfile.txt" , 0 , SW_SHOW );
如果我忽略notepad.exe参数,就会发生奇怪的事情(显示随机资源管理器)。

请参阅:

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

是否可以尝试启动而不是打开

如果lpFile指定一个文档文件,则只需将该标志传递给 关联应用程序

因此,您需要用要打开的实际文件替换
“c:\\windows\\notepad.exe”
,并保留
lpParameters
null。

根据,ShellExecute应该可以工作(我们一直在Delphi中执行此操作):


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

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

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

这里有更多的可能性:

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

std::wstring file=L“C:\\Outfile.txt”

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

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

open
是浏览器在双击文件时使用的默认动词。您可以将参数保留为NULL,以使用文件类型的实际默认值。如果您已经使其工作,问题是什么?OP没有“使其工作”。此“工作”版本仅使用记事本打开文件。其目的是让操作系统使用文件的关联程序打开文件,而不一定是记事本。例如:mspaint,不能使用“open”启动
ShellExecute(0, 0, L"c:\\outfile.txt", 0, 0 , SW_SHOW );
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);
//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);
std::wstring wsCmdExplorer = StringArgsW(L"/select,\"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"explorer.exe", wsCmdExplorer.c_str(), NULL, SW_NORMAL);