C++学习(八):删除文件

#include <Shlobj.h>
#include <iostream>
using namespace std;

//获取当前用户AppData文件夹路径
string  GetLocalAppDataPath(){
    char szDir[1024];
    char szDoc[1024];

    LPITEMIDLIST pidl = NULL;
    SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &pidl);
    if (pidl && SHGetPathFromIDList(pidl, szDoc)){
        GetShortPathName(szDoc, szDir, _MAX_PATH);
    }
    return szDir;
}

//获取当前用户
string GetLocalUserName(){    
    CHAR cUserNameBuffer[256];
    DWORD dwUserNameSize=256;

    GetUserName(cUserNameBuffer, &dwUserNameSize);
    return cUserNameBuffer;
}

int main(){
    string user = GetLocalUserName();
    string path = "C:/Users/" + user + "/AppData/Roaming/BeyondCompare/BeyondCompare419.ini";
    char *p = (char*)path.c_str();
    if (remove(p) == 0){
        cout << "删除成功" << endl;
    }
    else{
        cout << "删除失败" << endl;
    }
    system("pause");
    return 0;

}

 

posted @ 2019-09-17 15:00  liw66  阅读(1182)  评论(0编辑  收藏  举报