예제 #1
0
        /// <summary>
        /// 检测文件夹是否存在
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(父文件夹路径)</param>
        /// <param name="dirName">检测文件夹名称</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        /// <returns>检测情况</returns>
        public static EExistence CF_IsExistDirectory(ModFtpInfomation ftpInfomation, string dirName, out string errMsg)
        {
            EExistence result = EExistence.Error;

            try
            {
                List <ModFileInfo> files = CF_GetDirectory(ftpInfomation, out errMsg);

                if (files != null)
                {
                    if (files.Find(item => item.Name == dirName) != null)
                    {
                        result = EExistence.Exist;
                    }
                    else
                    {
                        result = EExistence.NotExist;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = EExistence.Error;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(父文件夹路径)</param>
        /// <param name="dirName">文件夹名称</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        /// <returns>删除情况</returns>
        public static bool CF_DeleteDirectory(ModFtpInfomation ftpInfomation, string dirName, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                EExistence isExist = CF_IsExistDirectory(ftpInfomation, dirName, out errMsg);

                if (isExist == EExistence.NotExist)
                {
                    errMsg = "目标文件夹不存在!";
                    result = false;
                }
                else if (isExist == EExistence.Exist)
                {
                    ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.RemoveDirectory;
                    ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(dirName);

                    FtpWebResponse ftpWebResponse = CF_GetFtpResponse(ftpInfomation, out errMsg);
                    if (ftpWebResponse != null)
                    {
                        ftpWebResponse.Close();

                        errMsg = "";
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
예제 #3
0
        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(父文件夹路径)</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        /// <returns>文件大小</returns>
        public static long CF_GetFileSize(ModFtpInfomation ftpInfomation, string fileName, out string errMsg)
        {
            long result = -1;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                EExistence isTarExist = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                if (isTarExist == EExistence.NotExist)
                {
                    errMsg = "目标文件不存在!";
                    result = -1;
                }
                else if (isTarExist == EExistence.Exist)
                {
                    ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.GetFileSize;
                    ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);

                    FtpWebResponse ftpWebResponse = CF_GetFtpResponse(ftpInfomation, out errMsg);
                    if (ftpWebResponse != null)
                    {
                        result = ftpWebResponse.ContentLength;
                        ftpWebResponse.Close();

                        errMsg = "";
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = -1;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
예제 #4
0
        /// <summary>
        /// 重命名文件夹
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(父文件夹路径)</param>
        /// <param name="orgDirName">原始文件夹名</param>
        /// <param name="tarDirName">目标文件夹名</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        /// <returns>重命名情况</returns>
        public static bool CF_RenameDirectory(ModFtpInfomation ftpInfomation, string orgDirName, string tarDirName, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                EExistence isOrgExist = CF_IsExistDirectory(ftpInfomation, orgDirName, out errMsg);

                if (isOrgExist == EExistence.NotExist)
                {
                    errMsg = "待重命名文件夹不存在!";
                    result = false;
                }
                else if (isOrgExist == EExistence.Exist)
                {
                    EExistence isTarExist = CF_IsExist(ftpInfomation, tarDirName, out errMsg);
                    if (isTarExist == EExistence.Exist)
                    {
                        errMsg = "已存在同名文件(夹)!";
                        result = false;
                    }
                    else if (isTarExist == EExistence.NotExist)
                    {
                        ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.Rename;
                        ftpInfomation.FtpReqInfo.RenameTo    = ftpInfomation.GetCombinePath(tarDirName);
                        ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(orgDirName);

                        FtpWebResponse ftpWebResponse = CF_GetFtpResponse(ftpInfomation, out errMsg);
                        if (ftpWebResponse != null)
                        {
                            FtpStatusCode statusCode;
                            using (ftpWebResponse.GetResponseStream())
                            {
                                statusCode = ftpWebResponse.StatusCode;
                            }

                            if (statusCode == FtpStatusCode.CommandOK || statusCode == FtpStatusCode.FileActionOK)
                            {
                                errMsg = "";
                                result = true;
                            }
                            else
                            {
                                errMsg = "文件夹重命名错误!";
                                result = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
예제 #5
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型</param>
        /// <param name="fileName">FTP文件名</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="overwrite">是否覆盖文件</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        public static bool CF_DownloadFile(ModFtpInfomation ftpInfomation, string fileName, string filePath, bool overwrite, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                //判断FTP文件是否存在
                EExistence isExistFile = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                if (isExistFile == EExistence.NotExist)
                {
                    errMsg = "FTP文件不存在!";
                    result = false;
                }
                else if (isExistFile == EExistence.Exist)
                {
                    FileInfo file = new FileInfo(filePath);

                    //创建本地文件夹
                    if (!file.Directory.Exists)
                    {
                        file.Directory.Create();
                    }

                    bool isExist = file.Exists;

                    if (isExist)
                    {
                        //是否覆盖
                        if (!overwrite)
                        {
                            errMsg = "目标文件已存在!";
                            result = false;
                        }
                        else
                        {
                            isExist = false;
                        }
                    }

                    if (!isExist)
                    {
                        ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.DownloadFile;
                        ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);
                        FtpWebResponse ftpWebResponse = CF_GetFtpResponse(ftpInfomation, out errMsg);

                        if (ftpWebResponse != null)
                        {
                            using (Stream stream = ftpWebResponse.GetResponseStream())
                            {
                                using (FileStream fileStream = new FileStream(file.FullName, FileMode.Create, FileAccess.Write))
                                {
                                    ModFtpTransSpeed transmissionSpeed = ftpInfomation.FtpReqInfo.DownloadSpeed;
                                    if (transmissionSpeed.EnableLimit)
                                    {
                                        //缓存字节数
                                        int bufferSize = transmissionSpeed.Speed * (int)Math.Pow(2, transmissionSpeed.Unit.CF_ToNumber());
                                        //缓存
                                        byte[] btBuffer = new byte[bufferSize];

                                        //读取的字节数
                                        int readSize = stream.Read(btBuffer, 0, bufferSize);
                                        while (readSize > 0)
                                        {
                                            //写入服务器
                                            fileStream.Write(btBuffer, 0, readSize);

                                            readSize = stream.Read(btBuffer, 0, bufferSize);

                                            //延时
                                            if (readSize > 0)
                                            {
                                                Thread.Sleep(transmissionSpeed.Delay);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        stream.CopyTo(fileStream);
                                    }
                                }
                            }
                        }

                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }
예제 #6
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="ftpInfomation">FTP基本信息模型(目标文件路径)</param>
        /// <param name="fileName">FTP文件名</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="overwrite">是否覆盖文件</param>
        /// <param name="errMsg">[OUT]错误信息</param>
        public static bool CF_UploadFile(ModFtpInfomation ftpInfomation, string fileName, string filePath, bool overwrite, out string errMsg)
        {
            bool result = false;

            ftpInfomation.FtpReqInfo.CF_PushRequestPath();

            try
            {
                //判断原始文件是否存在
                FileInfo file = new FileInfo(filePath);
                if (!file.Exists)
                {
                    errMsg = "本地文件不存在!";
                    result = false;
                }
                else
                {
                    EExistence isExistDir = CF_IsExistDirectory(ftpInfomation, out errMsg);
                    if (isExistDir == EExistence.Exist || string.IsNullOrEmpty(ftpInfomation.GetCurrentName()))
                    {
                        //判断目标文件是否存在
                        EExistence isExistFile = CF_IsExistFile(ftpInfomation, fileName, out errMsg);
                        if (isExistFile == EExistence.Exist)
                        {
                            //是否覆盖
                            if (!overwrite)
                            {
                                errMsg = "目标文件已存在!";
                                result = false;
                            }
                            else
                            {
                                isExistFile = EExistence.NotExist;
                            }
                        }

                        if (isExistFile == EExistence.NotExist)
                        {
                            ftpInfomation.FtpReqInfo.Method      = EFtpRequestMethod.UploadFile;
                            ftpInfomation.FtpReqInfo.RequestPath = ftpInfomation.GetCombinePath(fileName);
                            FtpWebRequest ftpWebRequest = CF_GetFtpRequest(ftpInfomation, out errMsg);

                            if (ftpWebRequest != null)
                            {
                                using (Stream stream = ftpWebRequest.GetRequestStream())
                                {
                                    using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                                    {
                                        ModFtpTransSpeed transmissionSpeed = ftpInfomation.FtpReqInfo.UploadSpeed;
                                        if (transmissionSpeed.EnableLimit)
                                        {
                                            //缓存字节数
                                            int bufferSize = transmissionSpeed.Speed * (int)Math.Pow(2, transmissionSpeed.Unit.CF_ToNumber());
                                            //缓存
                                            byte[] btBuffer = new byte[bufferSize];

                                            //读取的字节数
                                            int readSize = fileStream.Read(btBuffer, 0, bufferSize);
                                            while (readSize > 0)
                                            {
                                                //写入本地文件
                                                stream.Write(btBuffer, 0, readSize);

                                                readSize = fileStream.Read(btBuffer, 0, bufferSize);

                                                //延时
                                                if (readSize > 0)
                                                {
                                                    Thread.Sleep(transmissionSpeed.Delay);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            fileStream.CopyTo(stream);
                                        }
                                    }
                                }
                            }

                            result = true;
                        }
                    }
                    else
                    {
                        errMsg = "目标文件夹不存在!";
                        result = false;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                result = false;
            }

            ftpInfomation.FtpReqInfo.CF_PopRequestPath();
            return(result);
        }