简体   繁体   中英

How to get path of file dragged into Win32 app and delete it?

I have a program and when they drop files into it I want it to get the path show a messagebox "of the path" then delete it. Can anyone shed some light on how to do this?

//Create a window.
hWnd = CreateWindowEx
    WS_EX_ACCEPTFILES,      // Extended possibilites for variation.
    gsClassName,            // Classname.
    gsTitle,                // Title caption text.
    WS_OVERLAPPEDWINDOW,    // Default window.
    CW_USEDEFAULT,          // X Position.
    CW_USEDEFAULT,          // Y position.
    230,                    // Window starting width in pixils.
    150,                    // Window starting height in pixils.
    HWND_DESKTOP,           // The window is a child-window to desktop.
    (HMENU)NULL,            // No menu.
    hInstance,              // Program Instance handler.
    NULL                    // No Window Creation data.
);

if(uMessage == WM_DROPFILES)
{
    HDROP hDropInfo = (HDROP)wParam;
    char sItem[MAX_PATH];
    for(int i = 0; DragQueryFile(hDropInfo, i, (LPSTR)sItem, sizeof(sItem)); i++)
    {
        //Is the item a file or a directory?
        if(GetFileAttributes(sItem) &FILE_ATTRIBUTE_DIRECTORY)
        {
            //Delete all of the files in a directory before we can remove it.
            DeleteDirectoryRecursive(sItem);
        }
        else {
            SetFileAttributes(sItem, FILE_ATTRIBUTE_NORMAL); //Make file writable
            //DeleteFile(sItem);
        }

    }
    DragFinish(hDropInfo);
}

bool DeleteDirectoryRecursive(const char *sDir)
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    char sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    sprintf(sPath, "%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
        printf("Path not found: [%s]\n", sDir); 
        return false; 
    } 

    do 
    { 
        //Find first file will always return "."
        //    and ".." as the first two directories.
        if(strcmp(fdFile.cFileName, ".") != 0
                && strcmp(fdFile.cFileName, "..") != 0) 
        { 
            //Build up our file path using the passed in 
            //  [sDir] and the file/foldername we just found: 
            sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName); 

            //Is the entity a File or Folder? 
            if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
            { 
                DeleteDirectoryRecursive(sPath); //Recursive call.
            } 
            else{ 
                printf("File: %s\n", sPath);
                SetFileAttributes(sPath, FILE_ATTRIBUTE_NORMAL);
                //DeleteFile(sPath);
            } 
        } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    SetFileAttributes(sDir, FILE_ATTRIBUTE_NORMAL);
    //RemoveDirectory(sDir); //Delete the directory that was passed in.

    return true; 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM