首页 > 解决方案 > 在 C++ 中尝试从 Windows 10 中的 UNC 运行 EXE。C++ 非常新

问题描述

我刚开始用 C++ 编写控制台应用程序。它会进行一些验证,然后需要找到并运行一个可执行文件,该可执行文件可以位于不同位置,具体取决于它的部署方式。所以大部分脚本都可以工作,甚至运行部分也可以根据操作系统和位置工作。如果它是本地的,它可以工作,如果它是 Windows 7,它似乎甚至可以在 UNC 上工作。但在 Windows 10 中,它只是退出了。

该脚本找到 exe 并从它所在的路径运行它。当我将应用程序创建为批处理文件并使用 popD 移动到工作目录的 exe 位置时,它可以工作,但我似乎无法在 C++ 中模仿该功能. 我已经尝试过SetCurrentDirectory,但它不会带走我试图通过的字符串。

if (version >= minver)
    {
        std::string name = "testApp.exe";
        std::string path = (fs::current_path().string());
        for (const auto& entry : fs::recursive_directory_iterator(path))
        {
            std::string list = entry.path().string();
            int found;
            if ((found = list.find(name)) !=list.npos)
            {
                std::cout << list << std::endl;
\\This is the part that sometimes works and sometimes doesn't
                system(list.c_str());

            }
        }
    }   

标签: c++filesystems

解决方案


所以从长远来看,我以错误的方式追查问题。感谢 LandStalker 开辟了一条更好的道路。我对模仿 popD 的看法是正确的,但很难以正确的方式实际工作。
SetCurrentDirectory然后ShellExecute对程序名做了一个。


推荐阅读