Ejemplo n.º 1
0
        /// <summary>
        /// 下载目录
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="localpath"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public Int64 DownloadDirectory(FtpDirectory dir, String localpath, FtpTransportMode mode)
        {
            if (dir == null)
            {
                throw new ArgumentNullException("dir");
            }
            if (dir.Count < 1)
            {
                return(0);
            }
            if (String.IsNullOrEmpty(localpath))
            {
                throw new ArgumentNullException("localpath");
            }

            if (localpath[1] != Path.VolumeSeparatorChar)
            {
                localpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localpath);
            }

            Int64 count = 0;

            //遍历
            foreach (var item in dir)
            {
                if (item.IsDirectory)
                {
                    //递归
                    var d = ListDirectoryDetails(item.FullName);
                    count += DownloadDirectory(d, Path.Combine(localpath, item.FileName), mode);
                }
                else
                {
                    //下载文件
                    count += DownloadFile(item.FullName, Path.Combine(localpath, item.FileName), mode);
                }
            }
            return(count);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 上传目录
        /// </summary>
        /// <param name="localpath"></param>
        /// <param name="dir"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public Int64 UploadDirectory(String localpath, FtpDirectory dir, FtpTransportMode mode)
        {
            if (String.IsNullOrEmpty(localpath))
            {
                throw new ArgumentNullException("localpath");
            }
            if (dir == null)
            {
                throw new ArgumentNullException("dir");
            }

            if (localpath[1] != Path.VolumeSeparatorChar)
            {
                localpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localpath);
            }

            var   di   = new DirectoryInfo(localpath);
            Int64 size = 0;

            //遍历文件
            var files = di.GetFiles();

            if (files != null && files.Length > 0)
            {
                foreach (var item in files)
                {
                    //检查远端文件是否存在
                    if (dir.Contains(item.Name))
                    {
                        if (mode == FtpTransportMode.None)
                        {
                            continue;
                        }

                        //删除
                        if (mode == FtpTransportMode.OverWrite)
                        {
                            dir.Files[item.Name].Delete();
                        }
                    }

                    //上传文件
                    //UploadFile(item.FullName, f.FullName);
                    size += dir.UploadFile(item.FullName, mode);
                }
            }

            //遍历目录
            var dis = di.GetDirectories();

            if (dis != null && dis.Length > 0)
            {
                foreach (var item in dis)
                {
                    //检查远端文件夹是否存在
                    if (!dir.Contains(item.Name))
                    {
                        dir.CreateDirectory(item.Name);
                    }

                    //递归
                    size += UploadDirectory(item.FullName, dir.Directories[item.Name], mode);
                }
            }
            return(size);
        }