使用 Web 文件参数从 MVC 启动 Microsoft Edge

Starting Microsoft Edge from MFC with web file paramater

如果我打开一个控制台提示符,我可以输入这个命令:

start msedge "d:\HTML\Verticle Alignment.HTM"

它启动 Microsoft Edge 并打开网页。

所以我尝试在使用 MFC 的测试程序中以编程方式执行此操作:

void CMFCApplication8Dlg::OnBnClickedButton1()
{
    ExecuteProgram(_T("start"), _T("msedge d:\HTML\Verticle Alignment.HTM"));
}


BOOL CMFCApplication8Dlg::ExecuteProgram(CString strProgram, CString strArguments)
{
    SHELLEXECUTEINFO    se = { 0 };
    MSG                 sMessage;
    DWORD               dwResult;

    se.cbSize = sizeof(se);
    se.lpFile = strProgram;
    se.lpParameters = strArguments;
    se.nShow = SW_SHOWDEFAULT;
    se.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteEx(&se);

    if (se.hProcess != nullptr)
    {
        do
        {
            dwResult = ::MsgWaitForMultipleObjects(1, &(se.hProcess), FALSE,
                INFINITE, QS_ALLINPUT);
            if (dwResult != WAIT_OBJECT_0)
            {
                while (PeekMessage(&sMessage, nullptr, NULL, NULL, PM_REMOVE))
                {
                    TranslateMessage(&sMessage);
                    DispatchMessage(&sMessage);
                }
            }
        } while ((dwResult != WAIT_OBJECT_0) && (dwResult != WAIT_FAILED));

        CloseHandle(se.hProcess);
    }

    if ((DWORD_PTR)(se.hInstApp) < 33)
    {
        // Throw error
        AfxThrowUserException();
        return FALSE;
    }

    return TRUE;
}

但是当我 运行 它时,我收到此错误消息:

那么如何在 Microsoft Edge 中启动我的文件?我使用的是最新的 Windows 10,所以它是 Microsoft Edge Chromium.

我看到其他问题涉及将 Edge 设置为默认浏览器,然后只需“打开”数据文件即可解决所有问题,但在这种情况下这是不行的。在我的编辑器中,我有一个弹出菜单,其中列出了所有已安装的浏览器(目前不包括 Edge)。但我想添加 Edge,因此需要能够以编程方式启动它并使用我的文件进行查看。

根据提供给我的评论,并考虑到文件名中的空格,这是可行的:

ExecuteProgram(_T("msedge"), _T("\"d:/HTML/Verticle Alignment.HTM\""));
  • 要执行的程序必须是 msedge 而不是 start
  • 参数需要用 " 引号引起来。