2011-04-24 71 views
1

這會在桌面上創建一個新文件夾,但它不會將文件夾.pfrom的內容移動到文件夾.pTo。SHFileOperation不會移動文件夾的所有內容

int main() 
{ 

    SHFILEOPSTRUCT sf = {0}; 
    TCHAR myt[MAX_PATH]; 
    GetModuleFileName(NULL, myt, MAX_PATH); // puts the currente exe path in the buffer myt 
    string currentexepath; 
    int i; 

    for(i = 0; myt[i] != NULL; i++) { // this loop is for converting myt to string 
     currentexepath += myt[i];  // because string capabilities are needed 
    } 

    i = currentexepath.find_last_of("\\/"); 
    currentexepath = currentexepath.substr(0, i); 
    currentexepath += "\\subfolder\\*.*\0"; //i tried with and without *.* and \0 
    wstring ws = s2ws(currentexepath); 

    sf.wFunc = FO_COPY; 
    sf.hwnd = 0; 
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI; 
    sf.pFrom = ws.c_str(); 
    sf.pTo = L"C:\\Users\\Me\\Desktop\\folder\0"; 
    SHFileOperation(&sf); 
} 

// the following is from msdn 
// http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375 
wstring s2ws(const string& s) 
{ 
    int len; 
    int slength = (int)s.length() + 1; 
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len]; 
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); 
    std::wstring r(buf); 
    delete[] buf; 
    return r; 
} 

回答

1

SHFileOperation需要雙空終止的字符串。但你不能使用std :: string或std :: wstring。另見Double null-terminated string

當你這樣做:

currentexepath += "\\subfolder\\*.*\0"; 

字符串的+運算符沒有看到第二個空終止,因爲它停靠在第一個空。

這裏是你可以解決這個問題的方式:

int main() 
{ 
    SHFILEOPSTRUCT sf = {0}; 
    TCHAR myt[MAX_PATH]; 
    GetModuleFileName(NULL, myt, MAX_PATH); // puts the currente exe path in the buffer myt 
    string currentexepath; 

    if(TCHAR* LastSlash = _tcsrchr(myt, _T('\\'))) { 
     *LastSlash = _T('\0'); 
    } 

    // the pipe sign will be replaced with a \0 to get double null termination 
    // because _tcscat_s and all other strcat functions stop at the first \0 
    // we have to use this workaround 
    _tcscat_s(myt, _T("\\subfolder\\*.*|")); 
    while (TCHAR* ptr = _tcsrchr(myt, _T('|'))) { 
     *ptr = _T('\0'); 
    } 

    sf.wFunc = FO_COPY; 
    sf.hwnd = 0; 
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI; 
    sf.pFrom = myt; 
    sf.pTo = L"C:\\Users\\wh\\Desktop\\folder\0"; 
    if(SHFileOperation(&sf)!=0) { 
     // error occured 
     MessageBox(NULL, L"SHFileOperation failed", L"Error", MB_OK); 
    } 
} 

怎麼是,如果(),雖然()轉換成布爾語句?

例如這個if語句:

if(TCHAR* LastSlash = _tcsrchr(myt, _T('\\'))) { 
     *LastSlash = _T('\0'); 
    } 

也可以這樣寫:

TCHAR* LastSlash = _tcsrchr(myt, _T('\\')); 
    if(LastSlash) { 
     *LastSlash = _T('\0'); 
    } 

或:

TCHAR* LastSlash = _tcsrchr(myt, _T('\\')); 
    if(LastSlash != NULL) { 
     *LastSlash = _T('\0'); 
    } 

我結合TCHAR *和轉讓的簽入一份聲明。當一個指針被轉換爲一個布爾值時,NULL變爲false,並且所有其他值都變爲true。

+0

手動'push_back('\ 0');'會更簡單。 – Puppy 2011-04-24 10:42:23

+0

if()和while()語句如何轉換爲布爾值? – 2011-04-30 02:56:55

+0

@Geore,我在上面添加了一個解釋。如果還不清楚,請告訴我。 – wimh 2011-04-30 07:22:28

相關問題