Example #1
0
 /// <summary>
 /// Download tap tin strFile tai thu muc hien hanh tren FTP server
 /// Tham so:
 ///  strFile: ten file muon download
 /// Tra ve:
 ///  true: thanh cong
 ///  false: that bai
 /// </summary>
 public bool DownloadFile(String strFile)
 {
     return(CWinINet.FtpGetFile(m_HConnect, strFile, strFile,
                                true, FileAttribute.FILE_ATTRIBUTE_NORMAL, CWinINet.FTP_TRANSFER_TYPE_BINARY, 0));
 }
Example #2
0
    /// <summary>
    /// Download tat ca cac tap tin va thu muc con ben trong thu muc strFolder
    /// tren FTP server
    /// Tham so:
    ///  strFolder: ten thu muc muon download
    /// </summary>
    private bool _DownloadFolder(String strFolder)
    {
        StringBuilder   szPath       = new StringBuilder(256);
        String          arrFolder    = null;
        uint            uiLen        = 256;
        WIN32_FIND_DATA FindFileData = new WIN32_FIND_DATA();
        IntPtr          hFindFile;

        if (CWinINet.FtpSetCurrentDirectory(m_HConnect, strFolder) == false)
        {
            return(false);
        }

        try
        {
            Directory.SetCurrentDirectory(strFolder);
        }
        catch (Exception e)
        {
            return(false);
        }

        hFindFile = CWinINet.FtpFindFirstFile(m_HConnect, null, FindFileData,
                                              CWinINet.INTERNET_FLAG_RELOAD, 0);
        if (hFindFile.ToPointer() != null)
        {
            // Download tat ca cac tap tin ve may tram
            if (!((FindFileData.dwFileAttributes & FileAttribute.FILE_ATTRIBUTE_DIRECTORY) == FileAttribute.FILE_ATTRIBUTE_DIRECTORY))
            {
                if (!CWinINet.FtpGetFile(m_HConnect, FindFileData.cFileName, FindFileData.cFileName,
                                         true, FileAttribute.FILE_ATTRIBUTE_NORMAL, CWinINet.FTP_TRANSFER_TYPE_BINARY, 0))
                {
                    return(false);
                }
            }
            else // Thu muc
            {
                if (FindFileData.cFileName != ".")
                {
                    arrFolder += "|" + FindFileData.cFileName;
                }
            }

            while (CWinINet.InternetFindNextFile(hFindFile, FindFileData))
            {
                if (!((FindFileData.dwFileAttributes & FileAttribute.FILE_ATTRIBUTE_DIRECTORY) == FileAttribute.FILE_ATTRIBUTE_DIRECTORY))
                {
                    if (!CWinINet.FtpGetFile(m_HConnect, FindFileData.cFileName, FindFileData.cFileName,
                                             true, FileAttribute.FILE_ATTRIBUTE_NORMAL, CWinINet.FTP_TRANSFER_TYPE_BINARY, 0))
                    {
                        return(false);
                    }
                }
                else // Thu muc
                if (FindFileData.cFileName != "..")
                {
                    arrFolder += "|" + FindFileData.cFileName;
                }
            }
            if (CWinINet.InternetCloseHandle(hFindFile) == false)
            {
                return(false);
            }
            // Download tat ca cac thu muc ve may tram
            if (arrFolder != null)
            {
                string[] arr = arrFolder.Split('|');
                for (int i = 1; i < arr.Length; ++i)
                {
                    try
                    {
                        Directory.CreateDirectory(arr[i]);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }

                    return(_DownloadFolder(arr[i]));
                }
            }
            // Tra ve thu muc hien hanh
            uiLen = 256;
            if (CWinINet.FtpGetCurrentDirectory(m_HConnect, szPath, ref uiLen) == false)
            {
                return(false);
            }

            if (CWinINet.FtpSetCurrentDirectory(m_HConnect, szPath.ToString() + "//..") == false)
            {
                return(false);
            }

            try
            {
                Directory.SetCurrentDirectory(Directory.GetCurrentDirectory() + "\\..");
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        return(true);
    }