Beispiel #1
0
        /// <summary>
        /// 获取目录下的文件列表
        /// </summary>
        /// <param name="oFTPConfig"></param>
        /// <param name="alFileList"></param>
        /// <returns></returns>
        public bool GetRemoteFileList(FtpConfig oFTPConfig, ref ArrayList alFileList)
        {
            FtpLib    ftp = new FtpLib();
            string    strLocalFilePath  = @"" + oFTPConfig.strLocalFilePath;
            string    strRemoteFilePath = @"" + oFTPConfig.strRemoteFilePath;
            ArrayList myArraylist       = new ArrayList();

            try
            {
                ftp.port = int.Parse(oFTPConfig.strPort);
                ftp.Connect(oFTPConfig.strServer, oFTPConfig.strUserName, oFTPConfig.strPassword, int.Parse(oFTPConfig.strPort));
                ftp.ChangeDir(strRemoteFilePath);
                myArraylist = ftp.ListFiles();
                if (myArraylist.Count > 0)
                {
                    alFileList = GetFileName(myArraylist);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }
Beispiel #2
0
        /// <summary>
        /// 向FTP上传文件
        /// </summary>
        /// <param name="localPath">本地目录</param>
        /// <param name="fileName">文件名</param>
        /// <param name="errorinfo">报错信息</param>
        /// <param name="type">操作类型</param>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public static bool UploadFile(string localPath, string fileName, ref string errorinfo, string type, ref string filename)
        {
            FtpConfig ftpInfo = new FtpConfig(type);

            FtpLib ftp = new FtpLib();

            localPath = @"" + localPath + fileName;

            string remotePath = @"" + ftpInfo.strRemoteFilePath + fileName;

            int perc, old_perc = 0;

            if (!File.Exists(localPath))
            {
                errorinfo = string.Format("批开操作类型:{0},无法找到本地文件:{1}", type, localPath);

                return(false);
            }

            try
            {
                ftp.Connect(ftpInfo.strServer, ftpInfo.strUserName, ftpInfo.strPassword, int.Parse(ftpInfo.strPort));
                ftp.OpenUpload(localPath, remotePath, false);
                filename = ftpInfo.strRemoteAbsFile + "\\" + fileName;
                while (true)
                {
                    perc = ftp.DoUpload();
                    // No need to report progress everytime we get some bytes
                    // because it causes a flickery effect on the screen in most cases.
                    if (perc > old_perc)
                    {
                        Console.Write("\rFTP Complete: {0}%", perc);
                        Console.Out.Flush();
                    }
                    // is the download done?
                    if (perc == 100)
                    {
                        break;
                    }
                    old_perc = perc;
                }


                return(true);
            }
            catch (Exception ex)
            {
                errorinfo = string.Format("批开操作类型:{0},上传文件:{1},出现异常:{2}", type, localPath, ex.Message.ToString());

                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 上载本地文件到远程目录
        /// </summary>
        /// <param name="strFileName">文件名</param>
        /// <param name="oFTPConfig">配置类</param>
        /// <returns>布尔型</returns>
        public bool UploadSingleLocalFile(string strFileName, FtpConfig oFTPConfig)
        {
            string strLocalFileName  = @"" + oFTPConfig.strLocalFilePath + strFileName;
            string strRemoteFileName = @"" + oFTPConfig.strRemoteFilePath + strFileName;

            FtpLib ftp = new FtpLib();

            Console.WriteLine("【上载文件】 {0} 开始上载本地文件到远程目录 {1}", DateTime.Now.ToString(), strFileName);

            int perc, old_perc = 0;

            if (!File.Exists(strLocalFileName))
            {
                return(false);
            }

            try
            {
                ftp.Connect(oFTPConfig.strServer, oFTPConfig.strUserName, oFTPConfig.strPassword, int.Parse(oFTPConfig.strPort));

                ftp.OpenUpload(strLocalFileName, strRemoteFileName, false);

                while (true)
                {
                    perc = ftp.DoUpload();

                    // No need to report progress everytime we get some bytes
                    // because it causes a flickery effect on the screen in most cases.
                    if (perc > old_perc)
                    {
                        Console.Write("\r【上载进度】 FTP Complete: {0}%", perc);
                        Console.Out.Flush();
                    }
                    // is the download done?
                    if (perc == 100)
                    {
                        Console.WriteLine(" ");
                        break;
                    }
                    old_perc = perc;
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="oFTPConfig"></param>
        /// <param name="strTaskFileName"></param>
        /// <returns></returns>
        public bool ResponseSuccessToFtp(FtpConfig oFTPConfig, String strTaskFileName)
        {
            FtpLib ftp = new FtpLib();
            string strLocalFilePath = @"" + oFTPConfig.strLocalFilePath;

            strTaskFileName = strTaskFileName.Replace(".xml", "-" + oFTPConfig.strAreaName + ".ok");
            string strRemoteFilePath = @"" + oFTPConfig.strRemoteSuccessPath + strTaskFileName;

            strLocalFilePath += strTaskFileName;

            int perc, old_perc = 0;

            try
            {
                if (!File.Exists(strLocalFilePath))
                {
                    using (FileStream fs = (File.Create(strLocalFilePath))){}
                }

                ftp.Connect(oFTPConfig.strServer, oFTPConfig.strUserName, oFTPConfig.strPassword, int.Parse(oFTPConfig.strPort));
                ftp.OpenUpload(strLocalFilePath, strRemoteFilePath, false);

                while (true)
                {
                    perc = ftp.DoUpload();

                    // No need to report progress everytime we get some bytes
                    // because it causes a flickery effect on the screen in most cases.
                    if (perc > old_perc)
                    {
                        Console.Write("\rFTP Complete: {0}%", perc);
                        Console.Out.Flush();
                    }
                    // is the download done?
                    if (perc == 100)
                    {
                        break;
                    }

                    old_perc = perc;
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }