如何解决“文件保留原始位置并且在复制后不复制扩展名”的问题?

时间:2019-05-22 00:25:03

标签: c++ winapi

我正在尝试创建一个程序,该程序在运行到新位置时会自行复制,而不会保留原始文件的位置。复制后,我得到的文件没有扩展名,但是我该如何克服呢?

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR szFilepath[MAX_PATH];
    TCHAR szFilename[MAX_PATH];
    TCHAR szDestpath[MAX_PATH];

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

    /* Extract just the name */
    GetFileTitle(szFilepath, szFilename, MAX_PATH);
    std::wcout << "filename: " << szFilename << std::endl;

    //Set the destination folder path
    _tcscpy(szDestpath, L"D:\\");

    //Set the destination file path
    _tcscat(szDestpath, szFilename);

    std::wcout << "dest path: " << szDestpath << std::endl;

    // copys the file of your '.exe'

    if (!CopyFile(szFilepath, szDestpath, FALSE)) {
        std::cout << "couldnt copy the file";
    }
    else {
        std::cout << "copied";
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

根据GetFileTitle()的文档:

  

GetFileTitle返回系统用来向用户显示文件名的字符串。 显示名称仅包括扩展名,前提是该扩展名是用户显示文件名的首选项。这意味着,如果在调用文件系统功能时使用了返回的字符串,则可能无法准确识别该文件。

您应该使用更合适的函数来获取实际的文件名,例如PathFindFileName()

#include <windows.h>
#include <shlwapi.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    WCHAR szFilepath[MAX_PATH];
    LPWSTR lpszFilename;
    WCHAR szDestpath[MAX_PATH];

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

    /* Extract just the name */
    lpszFilename = PathFindFileNameW(szFilepath);
    std::wcout << L"filename: " << lpszFilename << std::endl;

    /* Set the destination folder path and file name */
    PathCombineW(szDestpath, L"D:\\", lpszFilename);
    std::wcout << L"dest path: " << szDestpath << std::endl;

    // copys the file of your '.exe'

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

或者,您可以简单地使用常规C ++字符串操作自己解析文件名,例如:

#include <windows.h>
#include <iostream>
#include <string>

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;
}