将正在运行的EXE移至新位置,而无需等待重新启动

时间:2019-05-22 01:40:30

标签: c++ windows

我正在尝试移动正在运行的EXE,但这给出了access denied because of running state。我可以使用MoveFileEx(MOVEFILE_DELAY_UNTIL_REBOOT),但这只有在重启后才能起作用。

有什么方法可以不用等待重启吗?

int _tmain(int argc, _TCHAR* argv[]){
    WCHAR szFilepath[MAX_PATH];
    std::wstring wFilepath;
    std::wstring wFilename;
    std::wstring wDestpath;

    /* Get the current executable's full path */
    wFilepath = std::wstring(szFilepath, GetModuleFileNameW(NULL, szFilepath, MAX_PATH));
    std::wcout << L"filepath: " << wFilepath << std::endl;

    /* Extract just the name */
    wFilename = wFilepath.substr(wFilepath.find_last_of(L"\\/") + 1);
    std::wcout << L"filename: " << wFilename << std::endl;

    /* Set the destination folder path and file name */
    wDestpath = L"D:\\" + wFilename;
    std::wcout << L"dest path: " << wDestpath << std::endl;

    // copys the file of your '.exe'

    if (!CopyFileW(wFilepath.c_str(), wDestpath.c_str(), FALSE)) {
        std::wcout << L"couldnt copy the file";
    }
    else {
        std::wcout << L"copied";
    }
    return 0;
}

0 个答案:

没有答案
相关问题