1207 AddConnectableRcs,可连接资源的变量,使用队列进行深度控制

来源:互联网 发布:游戏地图制作软件 编辑:程序博客网 时间:2024/05/20 13:06

DWORD AddConnectableRcs(LPNETRESOURCE lpConnectable, CTreeCtrl *pTree, HTREEITEM hItemParent, int maxLayer)

{

    // 1, WNetAddConnection2,使远程文件可以如同本地文件意义操作

    if (NO_ERROR != WNetAddConnection2 (lpConnectable, NULL, NULL, 0))

    {

        return -1;

    }

 

    // 2, 使用队列实现WPS

    CQueue queue;

    // 2.1 加入WPSroot

    CQueueNode qNode(lpConnectable->lpRemoteName, hItemParent, 0);

    queue.PutQueueObject(qNode);

   

    // 2.2 循环弹出CQueueNode,加入的都是目录,循环内部再对目录进行遍历

    while (!queue.IsEmpty())

    {

        // 2.2.1 弹出

        CQueueNode newNode = queue.GetQueueObject();

        CString dirName = newNode.GetString();

        HTREEITEM hItemParent = newNode.GetTreeItem();

        int newLayer = newNode.GetLayer();

 

        // 3 层次设置

        if (newLayer > maxLayer)

        {

            break;

        }

 

        // 4 遍历一个目录的方法

        // 4.1 SetCurrentDirectory

        if (!SetCurrentDirectory(dirName))

        {

            DWORD dwErrorCode = GetLastError();

            printf ("SetCurrentDirectory. Error is %u/n", dwErrorCode);

            return (-1);

        }

       

        WIN32_FIND_DATA FindFileData;

        HANDLE hFind;

        DWORD dwError;

 

        // 4.2 FindFirstFile("*", &FindFileData)

        hFind = FindFirstFile("*", &FindFileData);

        if (hFind == INVALID_HANDLE_VALUE)

        {

            DWORD dwErrorCode = GetLastError();

            printf ("Invalid file handle. Error is %u/n", dwErrorCode);

            return (-1);

        }

        else

        {

            // 第一个文件是“.

            printf ("First file name is %s/n", FindFileData.cFileName);

 

            // 4.3 步进

            // 第二个文件是“..”,也不需要加入树结构

            FindNextFile(hFind, &FindFileData);

 

            // 其他加入结构

            while (FindNextFile(hFind, &FindFileData) != 0)

            {

                // 4.5 文件操作函数,判断“文件夹”属性的方法

                // 当前的file是一个目录

                         if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                         {

                    // 4.6 去文件名的方法

                    HTREEITEM hItemDir = pTree->InsertItem(FindFileData.cFileName, 0, 1, hItemParent, TVI_LAST);

 

                    // 2.3 压栈

                    CQueueNode childNode(dirName + "/" + FindFileData.cFileName, hItemDir, newLayer + 1);

                    queue.PutQueueObject(childNode);

                         }

                         else

                         {

                    pTree->InsertItem(FindFileData.cFileName,0,1, hItemParent, TVI_LAST);

                         }

            }

 

            dwError = GetLastError();

            // 4.7 文件操作函数系列,Close

            FindClose(hFind);

            if (dwError != ERROR_NO_MORE_FILES)

            {

                printf ("FindNextFile error. Error is %u/n", dwError);

                return (-1);

            }

        }

    }

 

    // 5 有开有关,断开远程连接

    if (NO_ERROR != WNetCancelConnection2(lpConnectable->lpRemoteName, 0, TRUE))

    {

        // error print

        cout << "WNetCancelConnection2 Failed!/n/n";

        return -1;

    }

 

    return 0;

}

原创粉丝点击